x64, x2apic/intr-remap: x2apic ops for x2apic mode support
[deliverable/linux.git] / arch / x86 / kernel / apic_64.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>
ba7eda4c 27#include <linux/clockchips.h>
70a20025 28#include <linux/acpi_pmtmr.h>
e83a5fdc 29#include <linux/module.h>
1da177e4
LT
30
31#include <asm/atomic.h>
32#include <asm/smp.h>
33#include <asm/mtrr.h>
34#include <asm/mpspec.h>
e83a5fdc 35#include <asm/hpet.h>
1da177e4 36#include <asm/pgalloc.h>
75152114 37#include <asm/nmi.h>
95833c83 38#include <asm/idle.h>
73dea47f
AK
39#include <asm/proto.h>
40#include <asm/timex.h>
2c8c0e6b 41#include <asm/apic.h>
1da177e4 42
5af5573e 43#include <mach_ipi.h>
dd46e3ca 44#include <mach_apic.h>
5af5573e 45
aa276e1c 46static int disable_apic_timer __cpuinitdata;
bc1d99c1 47static int apic_calibrate_pmtmr __initdata;
0e078e2f 48int disable_apic;
1da177e4 49
e83a5fdc 50/* Local APIC timer works in C2 */
2e7c2838
LT
51int local_apic_timer_c2_ok;
52EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
53
e83a5fdc
HS
54/*
55 * Debug level, exported for io_apic.c
56 */
57int apic_verbosity;
58
bab4b27c
AS
59/* Have we found an MP table */
60int smp_found_config;
61
39928722
AD
62static struct resource lapic_resource = {
63 .name = "Local APIC",
64 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
65};
66
d03030e9
TG
67static unsigned int calibration_result;
68
ba7eda4c
TG
69static int lapic_next_event(unsigned long delta,
70 struct clock_event_device *evt);
71static void lapic_timer_setup(enum clock_event_mode mode,
72 struct clock_event_device *evt);
ba7eda4c 73static void lapic_timer_broadcast(cpumask_t mask);
0e078e2f 74static void apic_pm_activate(void);
ba7eda4c
TG
75
76static struct clock_event_device lapic_clockevent = {
77 .name = "lapic",
78 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
79 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
80 .shift = 32,
81 .set_mode = lapic_timer_setup,
82 .set_next_event = lapic_next_event,
83 .broadcast = lapic_timer_broadcast,
84 .rating = 100,
85 .irq = -1,
86};
87static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
88
d3432896
AK
89static unsigned long apic_phys;
90
3f530709
AS
91unsigned long mp_lapic_addr;
92
be8a5685 93unsigned int __cpuinitdata maxcpus = NR_CPUS;
0e078e2f
TG
94/*
95 * Get the LAPIC version
96 */
97static inline int lapic_get_version(void)
ba7eda4c 98{
0e078e2f 99 return GET_APIC_VERSION(apic_read(APIC_LVR));
ba7eda4c
TG
100}
101
0e078e2f
TG
102/*
103 * Check, if the APIC is integrated or a seperate chip
104 */
105static inline int lapic_is_integrated(void)
ba7eda4c 106{
0e078e2f 107 return 1;
ba7eda4c
TG
108}
109
110/*
0e078e2f 111 * Check, whether this is a modern or a first generation APIC
ba7eda4c 112 */
0e078e2f 113static int modern_apic(void)
ba7eda4c 114{
0e078e2f
TG
115 /* AMD systems use old APIC versions, so check the CPU */
116 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
117 boot_cpu_data.x86 >= 0xf)
118 return 1;
119 return lapic_get_version() >= 0x14;
ba7eda4c
TG
120}
121
1b374e4d 122void xapic_wait_icr_idle(void)
8339e9fb
FLV
123{
124 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
125 cpu_relax();
126}
127
1b374e4d 128u32 safe_xapic_wait_icr_idle(void)
8339e9fb 129{
3c6bb07a 130 u32 send_status;
8339e9fb
FLV
131 int timeout;
132
133 timeout = 0;
134 do {
135 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
136 if (!send_status)
137 break;
138 udelay(100);
139 } while (timeout++ < 1000);
140
141 return send_status;
142}
143
1b374e4d
SS
144void xapic_icr_write(u32 low, u32 id)
145{
146 apic_write(APIC_ICR2, id << 24);
147 apic_write(APIC_ICR, low);
148}
149
150u64 xapic_icr_read(void)
151{
152 u32 icr1, icr2;
153
154 icr2 = apic_read(APIC_ICR2);
155 icr1 = apic_read(APIC_ICR);
156
157 return (icr1 | ((u64)icr2 << 32));
158}
159
160static struct apic_ops xapic_ops = {
161 .read = native_apic_mem_read,
162 .write = native_apic_mem_write,
163 .write_atomic = native_apic_mem_write_atomic,
164 .icr_read = xapic_icr_read,
165 .icr_write = xapic_icr_write,
166 .wait_icr_idle = xapic_wait_icr_idle,
167 .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
168};
169
170struct apic_ops __read_mostly *apic_ops = &xapic_ops;
171
172EXPORT_SYMBOL_GPL(apic_ops);
173
13c88fb5
SS
174static void x2apic_wait_icr_idle(void)
175{
176 /* no need to wait for icr idle in x2apic */
177 return;
178}
179
180static u32 safe_x2apic_wait_icr_idle(void)
181{
182 /* no need to wait for icr idle in x2apic */
183 return 0;
184}
185
186void x2apic_icr_write(u32 low, u32 id)
187{
188 wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
189}
190
191u64 x2apic_icr_read(void)
192{
193 unsigned long val;
194
195 rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
196 return val;
197}
198
199static struct apic_ops x2apic_ops = {
200 .read = native_apic_msr_read,
201 .write = native_apic_msr_write,
202 .write_atomic = native_apic_msr_write,
203 .icr_read = x2apic_icr_read,
204 .icr_write = x2apic_icr_write,
205 .wait_icr_idle = x2apic_wait_icr_idle,
206 .safe_wait_icr_idle = safe_x2apic_wait_icr_idle,
207};
208
0e078e2f
TG
209/**
210 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
211 */
e9427101 212void __cpuinit enable_NMI_through_LVT0(void)
1da177e4 213{
11a8e778 214 unsigned int v;
6935d1f9
TG
215
216 /* unmask and set to NMI */
217 v = APIC_DM_NMI;
11a8e778 218 apic_write(APIC_LVT0, v);
1da177e4
LT
219}
220
0e078e2f
TG
221/**
222 * lapic_get_maxlvt - get the maximum number of local vector table entries
223 */
37e650c7 224int lapic_get_maxlvt(void)
1da177e4 225{
11a8e778 226 unsigned int v, maxlvt;
1da177e4
LT
227
228 v = apic_read(APIC_LVR);
1da177e4
LT
229 maxlvt = GET_APIC_MAXLVT(v);
230 return maxlvt;
231}
232
0e078e2f
TG
233/*
234 * This function sets up the local APIC timer, with a timeout of
235 * 'clocks' APIC bus clock. During calibration we actually call
236 * this function twice on the boot CPU, once with a bogus timeout
237 * value, second time for real. The other (noncalibrating) CPUs
238 * call this function only once, with the real, calibrated value.
239 *
240 * We do reads before writes even if unnecessary, to get around the
241 * P5 APIC double write bug.
242 */
243
244static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
1da177e4 245{
0e078e2f 246 unsigned int lvtt_value, tmp_value;
1da177e4 247
0e078e2f
TG
248 lvtt_value = LOCAL_TIMER_VECTOR;
249 if (!oneshot)
250 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
251 if (!irqen)
252 lvtt_value |= APIC_LVT_MASKED;
1da177e4 253
0e078e2f 254 apic_write(APIC_LVTT, lvtt_value);
1da177e4
LT
255
256 /*
0e078e2f 257 * Divide PICLK by 16
1da177e4 258 */
0e078e2f
TG
259 tmp_value = apic_read(APIC_TDCR);
260 apic_write(APIC_TDCR, (tmp_value
261 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
262 | APIC_TDR_DIV_16);
263
264 if (!oneshot)
265 apic_write(APIC_TMICT, clocks);
1da177e4
LT
266}
267
0e078e2f 268/*
7b83dae7
RR
269 * Setup extended LVT, AMD specific (K8, family 10h)
270 *
271 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
272 * MCE interrupts are supported. Thus MCE offset must be set to 0.
0e078e2f 273 */
7b83dae7
RR
274
275#define APIC_EILVT_LVTOFF_MCE 0
276#define APIC_EILVT_LVTOFF_IBS 1
277
278static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
1da177e4 279{
7b83dae7 280 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
0e078e2f 281 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
a8fcf1a2 282
0e078e2f 283 apic_write(reg, v);
1da177e4
LT
284}
285
7b83dae7
RR
286u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
287{
288 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
289 return APIC_EILVT_LVTOFF_MCE;
290}
291
292u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
293{
294 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
295 return APIC_EILVT_LVTOFF_IBS;
296}
297
0e078e2f
TG
298/*
299 * Program the next event, relative to now
300 */
301static int lapic_next_event(unsigned long delta,
302 struct clock_event_device *evt)
1da177e4 303{
0e078e2f
TG
304 apic_write(APIC_TMICT, delta);
305 return 0;
1da177e4
LT
306}
307
0e078e2f
TG
308/*
309 * Setup the lapic timer in periodic or oneshot mode
310 */
311static void lapic_timer_setup(enum clock_event_mode mode,
312 struct clock_event_device *evt)
9b7711f0
HS
313{
314 unsigned long flags;
0e078e2f 315 unsigned int v;
9b7711f0 316
0e078e2f
TG
317 /* Lapic used as dummy for broadcast ? */
318 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
9b7711f0
HS
319 return;
320
321 local_irq_save(flags);
322
0e078e2f
TG
323 switch (mode) {
324 case CLOCK_EVT_MODE_PERIODIC:
325 case CLOCK_EVT_MODE_ONESHOT:
326 __setup_APIC_LVTT(calibration_result,
327 mode != CLOCK_EVT_MODE_PERIODIC, 1);
328 break;
329 case CLOCK_EVT_MODE_UNUSED:
330 case CLOCK_EVT_MODE_SHUTDOWN:
331 v = apic_read(APIC_LVTT);
332 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
333 apic_write(APIC_LVTT, v);
334 break;
335 case CLOCK_EVT_MODE_RESUME:
336 /* Nothing to do here */
337 break;
338 }
9b7711f0
HS
339
340 local_irq_restore(flags);
341}
342
1da177e4 343/*
0e078e2f 344 * Local APIC timer broadcast function
1da177e4 345 */
0e078e2f 346static void lapic_timer_broadcast(cpumask_t mask)
1da177e4 347{
0e078e2f
TG
348#ifdef CONFIG_SMP
349 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
350#endif
351}
1da177e4 352
0e078e2f
TG
353/*
354 * Setup the local APIC timer for this CPU. Copy the initilized values
355 * of the boot CPU and register the clock event in the framework.
356 */
357static void setup_APIC_timer(void)
358{
359 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
1da177e4 360
0e078e2f
TG
361 memcpy(levt, &lapic_clockevent, sizeof(*levt));
362 levt->cpumask = cpumask_of_cpu(smp_processor_id());
1da177e4 363
0e078e2f
TG
364 clockevents_register_device(levt);
365}
1da177e4 366
0e078e2f
TG
367/*
368 * In this function we calibrate APIC bus clocks to the external
369 * timer. Unfortunately we cannot use jiffies and the timer irq
370 * to calibrate, since some later bootup code depends on getting
371 * the first irq? Ugh.
372 *
373 * We want to do the calibration only once since we
374 * want to have local timer irqs syncron. CPUs connected
375 * by the same APIC bus have the very same bus frequency.
376 * And we want to have irqs off anyways, no accidental
377 * APIC irq that way.
378 */
379
380#define TICK_COUNT 100000000
381
382static void __init calibrate_APIC_clock(void)
383{
384 unsigned apic, apic_start;
385 unsigned long tsc, tsc_start;
386 int result;
387
388 local_irq_disable();
389
390 /*
391 * Put whatever arbitrary (but long enough) timeout
392 * value into the APIC clock, we just want to get the
393 * counter running for calibration.
394 *
395 * No interrupt enable !
396 */
397 __setup_APIC_LVTT(250000000, 0, 0);
398
399 apic_start = apic_read(APIC_TMCCT);
400#ifdef CONFIG_X86_PM_TIMER
401 if (apic_calibrate_pmtmr && pmtmr_ioport) {
402 pmtimer_wait(5000); /* 5ms wait */
403 apic = apic_read(APIC_TMCCT);
404 result = (apic_start - apic) * 1000L / 5;
405 } else
406#endif
407 {
408 rdtscll(tsc_start);
409
410 do {
411 apic = apic_read(APIC_TMCCT);
412 rdtscll(tsc);
413 } while ((tsc - tsc_start) < TICK_COUNT &&
414 (apic_start - apic) < TICK_COUNT);
415
416 result = (apic_start - apic) * 1000L * tsc_khz /
417 (tsc - tsc_start);
418 }
419
420 local_irq_enable();
421
422 printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
423
424 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
425 result / 1000 / 1000, result / 1000 % 1000);
426
427 /* Calculate the scaled math multiplication factor */
877084fb
AM
428 lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC,
429 lapic_clockevent.shift);
0e078e2f
TG
430 lapic_clockevent.max_delta_ns =
431 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
432 lapic_clockevent.min_delta_ns =
433 clockevent_delta2ns(0xF, &lapic_clockevent);
434
435 calibration_result = result / HZ;
436}
437
e83a5fdc
HS
438/*
439 * Setup the boot APIC
440 *
441 * Calibrate and verify the result.
442 */
0e078e2f
TG
443void __init setup_boot_APIC_clock(void)
444{
445 /*
446 * The local apic timer can be disabled via the kernel commandline.
447 * Register the lapic timer as a dummy clock event source on SMP
448 * systems, so the broadcast mechanism is used. On UP systems simply
449 * ignore it.
450 */
451 if (disable_apic_timer) {
452 printk(KERN_INFO "Disabling APIC timer\n");
453 /* No broadcast on UP ! */
9d09951d
TG
454 if (num_possible_cpus() > 1) {
455 lapic_clockevent.mult = 1;
0e078e2f 456 setup_APIC_timer();
9d09951d 457 }
0e078e2f
TG
458 return;
459 }
460
461 printk(KERN_INFO "Using local APIC timer interrupts.\n");
462 calibrate_APIC_clock();
463
c2b84b30
TG
464 /*
465 * Do a sanity check on the APIC calibration result
466 */
467 if (calibration_result < (1000000 / HZ)) {
468 printk(KERN_WARNING
469 "APIC frequency too slow, disabling apic timer\n");
470 /* No broadcast on UP ! */
471 if (num_possible_cpus() > 1)
472 setup_APIC_timer();
473 return;
474 }
475
0e078e2f
TG
476 /*
477 * If nmi_watchdog is set to IO_APIC, we need the
478 * PIT/HPET going. Otherwise register lapic as a dummy
479 * device.
480 */
481 if (nmi_watchdog != NMI_IO_APIC)
482 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
483 else
484 printk(KERN_WARNING "APIC timer registered as dummy,"
116f570e 485 " due to nmi_watchdog=%d!\n", nmi_watchdog);
0e078e2f
TG
486
487 setup_APIC_timer();
488}
489
0e078e2f
TG
490void __cpuinit setup_secondary_APIC_clock(void)
491{
0e078e2f
TG
492 setup_APIC_timer();
493}
494
495/*
496 * The guts of the apic timer interrupt
497 */
498static void local_apic_timer_interrupt(void)
499{
500 int cpu = smp_processor_id();
501 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
502
503 /*
504 * Normally we should not be here till LAPIC has been initialized but
505 * in some cases like kdump, its possible that there is a pending LAPIC
506 * timer interrupt from previous kernel's context and is delivered in
507 * new kernel the moment interrupts are enabled.
508 *
509 * Interrupts are enabled early and LAPIC is setup much later, hence
510 * its possible that when we get here evt->event_handler is NULL.
511 * Check for event_handler being NULL and discard the interrupt as
512 * spurious.
513 */
514 if (!evt->event_handler) {
515 printk(KERN_WARNING
516 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
517 /* Switch it off */
518 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
519 return;
520 }
521
522 /*
523 * the NMI deadlock-detector uses this.
524 */
525 add_pda(apic_timer_irqs, 1);
526
527 evt->event_handler(evt);
528}
529
530/*
531 * Local APIC timer interrupt. This is the most natural way for doing
532 * local interrupts, but local timer interrupts can be emulated by
533 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
534 *
535 * [ if a single-CPU system runs an SMP kernel then we call the local
536 * interrupt as well. Thus we cannot inline the local irq ... ]
537 */
538void smp_apic_timer_interrupt(struct pt_regs *regs)
539{
540 struct pt_regs *old_regs = set_irq_regs(regs);
541
542 /*
543 * NOTE! We'd better ACK the irq immediately,
544 * because timer handling can be slow.
545 */
546 ack_APIC_irq();
547 /*
548 * update_process_times() expects us to have done irq_enter().
549 * Besides, if we don't timer interrupts ignore the global
550 * interrupt lock, which is the WrongThing (tm) to do.
551 */
552 exit_idle();
553 irq_enter();
554 local_apic_timer_interrupt();
555 irq_exit();
556 set_irq_regs(old_regs);
557}
558
559int setup_profiling_timer(unsigned int multiplier)
560{
561 return -EINVAL;
562}
563
564
565/*
566 * Local APIC start and shutdown
567 */
568
569/**
570 * clear_local_APIC - shutdown the local APIC
571 *
572 * This is called, when a CPU is disabled and before rebooting, so the state of
573 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
574 * leftovers during boot.
575 */
576void clear_local_APIC(void)
577{
2584a82d 578 int maxlvt;
0e078e2f
TG
579 u32 v;
580
d3432896
AK
581 /* APIC hasn't been mapped yet */
582 if (!apic_phys)
583 return;
584
585 maxlvt = lapic_get_maxlvt();
0e078e2f
TG
586 /*
587 * Masking an LVT entry can trigger a local APIC error
588 * if the vector is zero. Mask LVTERR first to prevent this.
589 */
590 if (maxlvt >= 3) {
591 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
592 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
593 }
594 /*
595 * Careful: we have to set masks only first to deassert
596 * any level-triggered sources.
597 */
598 v = apic_read(APIC_LVTT);
599 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
600 v = apic_read(APIC_LVT0);
601 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
602 v = apic_read(APIC_LVT1);
603 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
604 if (maxlvt >= 4) {
605 v = apic_read(APIC_LVTPC);
606 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
607 }
608
609 /*
610 * Clean APIC state for other OSs:
611 */
612 apic_write(APIC_LVTT, APIC_LVT_MASKED);
613 apic_write(APIC_LVT0, APIC_LVT_MASKED);
614 apic_write(APIC_LVT1, APIC_LVT_MASKED);
615 if (maxlvt >= 3)
616 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
617 if (maxlvt >= 4)
618 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
619 apic_write(APIC_ESR, 0);
620 apic_read(APIC_ESR);
621}
622
623/**
624 * disable_local_APIC - clear and disable the local APIC
625 */
626void disable_local_APIC(void)
627{
628 unsigned int value;
629
630 clear_local_APIC();
631
632 /*
633 * Disable APIC (implies clearing of registers
634 * for 82489DX!).
635 */
636 value = apic_read(APIC_SPIV);
637 value &= ~APIC_SPIV_APIC_ENABLED;
638 apic_write(APIC_SPIV, value);
639}
640
641void lapic_shutdown(void)
642{
643 unsigned long flags;
644
645 if (!cpu_has_apic)
646 return;
647
648 local_irq_save(flags);
649
650 disable_local_APIC();
651
652 local_irq_restore(flags);
653}
654
655/*
656 * This is to verify that we're looking at a real local APIC.
657 * Check these against your board if the CPUs aren't getting
658 * started for no apparent reason.
659 */
660int __init verify_local_APIC(void)
661{
662 unsigned int reg0, reg1;
663
664 /*
665 * The version register is read-only in a real APIC.
666 */
667 reg0 = apic_read(APIC_LVR);
668 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
669 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
670 reg1 = apic_read(APIC_LVR);
671 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
672
673 /*
674 * The two version reads above should print the same
675 * numbers. If the second one is different, then we
676 * poke at a non-APIC.
677 */
678 if (reg1 != reg0)
679 return 0;
680
681 /*
682 * Check if the version looks reasonably.
683 */
684 reg1 = GET_APIC_VERSION(reg0);
685 if (reg1 == 0x00 || reg1 == 0xff)
686 return 0;
687 reg1 = lapic_get_maxlvt();
688 if (reg1 < 0x02 || reg1 == 0xff)
689 return 0;
690
691 /*
692 * The ID register is read/write in a real APIC.
693 */
2d7a66d0 694 reg0 = apic_read(APIC_ID);
0e078e2f
TG
695 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
696 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
2d7a66d0 697 reg1 = apic_read(APIC_ID);
0e078e2f
TG
698 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
699 apic_write(APIC_ID, reg0);
700 if (reg1 != (reg0 ^ APIC_ID_MASK))
701 return 0;
702
703 /*
1da177e4
LT
704 * The next two are just to see if we have sane values.
705 * They're only really relevant if we're in Virtual Wire
706 * compatibility mode, but most boxes are anymore.
707 */
708 reg0 = apic_read(APIC_LVT0);
0e078e2f 709 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
1da177e4
LT
710 reg1 = apic_read(APIC_LVT1);
711 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
712
713 return 1;
714}
715
0e078e2f
TG
716/**
717 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
718 */
1da177e4
LT
719void __init sync_Arb_IDs(void)
720{
721 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
0e078e2f 722 if (modern_apic())
1da177e4
LT
723 return;
724
725 /*
726 * Wait for idle.
727 */
728 apic_wait_icr_idle();
729
730 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
11a8e778 731 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
1da177e4
LT
732 | APIC_DM_INIT);
733}
734
1da177e4
LT
735/*
736 * An initial setup of the virtual wire mode.
737 */
738void __init init_bsp_APIC(void)
739{
11a8e778 740 unsigned int value;
1da177e4
LT
741
742 /*
743 * Don't do the setup now if we have a SMP BIOS as the
744 * through-I/O-APIC virtual wire mode might be active.
745 */
746 if (smp_found_config || !cpu_has_apic)
747 return;
748
749 value = apic_read(APIC_LVR);
1da177e4
LT
750
751 /*
752 * Do not trust the local APIC being empty at bootup.
753 */
754 clear_local_APIC();
755
756 /*
757 * Enable APIC.
758 */
759 value = apic_read(APIC_SPIV);
760 value &= ~APIC_VECTOR_MASK;
761 value |= APIC_SPIV_APIC_ENABLED;
762 value |= APIC_SPIV_FOCUS_DISABLED;
763 value |= SPURIOUS_APIC_VECTOR;
11a8e778 764 apic_write(APIC_SPIV, value);
1da177e4
LT
765
766 /*
767 * Set up the virtual wire mode.
768 */
11a8e778 769 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1da177e4 770 value = APIC_DM_NMI;
11a8e778 771 apic_write(APIC_LVT1, value);
1da177e4
LT
772}
773
0e078e2f
TG
774/**
775 * setup_local_APIC - setup the local APIC
776 */
777void __cpuinit setup_local_APIC(void)
1da177e4 778{
739f33b3 779 unsigned int value;
da7ed9f9 780 int i, j;
1da177e4 781
ac23d4ee 782 preempt_disable();
1da177e4 783 value = apic_read(APIC_LVR);
1da177e4 784
fe7414a2 785 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
1da177e4
LT
786
787 /*
788 * Double-check whether this APIC is really registered.
789 * This is meaningless in clustered apic mode, so we skip it.
790 */
791 if (!apic_id_registered())
792 BUG();
793
794 /*
795 * Intel recommends to set DFR, LDR and TPR before enabling
796 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
797 * document number 292116). So here it goes...
798 */
799 init_apic_ldr();
800
801 /*
802 * Set Task Priority to 'accept all'. We never change this
803 * later on.
804 */
805 value = apic_read(APIC_TASKPRI);
806 value &= ~APIC_TPRI_MASK;
11a8e778 807 apic_write(APIC_TASKPRI, value);
1da177e4 808
da7ed9f9
VG
809 /*
810 * After a crash, we no longer service the interrupts and a pending
811 * interrupt from previous kernel might still have ISR bit set.
812 *
813 * Most probably by now CPU has serviced that pending interrupt and
814 * it might not have done the ack_APIC_irq() because it thought,
815 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
816 * does not clear the ISR bit and cpu thinks it has already serivced
817 * the interrupt. Hence a vector might get locked. It was noticed
818 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
819 */
820 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
821 value = apic_read(APIC_ISR + i*0x10);
822 for (j = 31; j >= 0; j--) {
823 if (value & (1<<j))
824 ack_APIC_irq();
825 }
826 }
827
1da177e4
LT
828 /*
829 * Now that we are all set up, enable the APIC
830 */
831 value = apic_read(APIC_SPIV);
832 value &= ~APIC_VECTOR_MASK;
833 /*
834 * Enable APIC
835 */
836 value |= APIC_SPIV_APIC_ENABLED;
837
3f14c746
AK
838 /* We always use processor focus */
839
1da177e4
LT
840 /*
841 * Set spurious IRQ vector
842 */
843 value |= SPURIOUS_APIC_VECTOR;
11a8e778 844 apic_write(APIC_SPIV, value);
1da177e4
LT
845
846 /*
847 * Set up LVT0, LVT1:
848 *
849 * set up through-local-APIC on the BP's LINT0. This is not
850 * strictly necessary in pure symmetric-IO mode, but sometimes
851 * we delegate interrupts to the 8259A.
852 */
853 /*
854 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
855 */
856 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
a8fcf1a2 857 if (!smp_processor_id() && !value) {
1da177e4 858 value = APIC_DM_EXTINT;
bc1d99c1
CW
859 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
860 smp_processor_id());
1da177e4
LT
861 } else {
862 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
bc1d99c1
CW
863 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
864 smp_processor_id());
1da177e4 865 }
11a8e778 866 apic_write(APIC_LVT0, value);
1da177e4
LT
867
868 /*
869 * only the BP should see the LINT1 NMI signal, obviously.
870 */
871 if (!smp_processor_id())
872 value = APIC_DM_NMI;
873 else
874 value = APIC_DM_NMI | APIC_LVT_MASKED;
11a8e778 875 apic_write(APIC_LVT1, value);
ac23d4ee 876 preempt_enable();
739f33b3 877}
1da177e4 878
a4928cff 879static void __cpuinit lapic_setup_esr(void)
739f33b3
AK
880{
881 unsigned maxlvt = lapic_get_maxlvt();
882
883 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR);
1c69524c 884 /*
739f33b3 885 * spec says clear errors after enabling vector.
1c69524c 886 */
739f33b3
AK
887 if (maxlvt > 3)
888 apic_write(APIC_ESR, 0);
889}
1da177e4 890
739f33b3
AK
891void __cpuinit end_local_APIC_setup(void)
892{
893 lapic_setup_esr();
f2802e7f 894 setup_apic_nmi_watchdog(NULL);
0e078e2f 895 apic_pm_activate();
1da177e4 896}
1da177e4
LT
897
898/*
899 * Detect and enable local APICs on non-SMP boards.
900 * Original code written by Keir Fraser.
901 * On AMD64 we trust the BIOS - if it says no APIC it is likely
6935d1f9 902 * not correctly set up (usually the APIC timer won't work etc.)
1da177e4 903 */
0e078e2f 904static int __init detect_init_APIC(void)
1da177e4
LT
905{
906 if (!cpu_has_apic) {
907 printk(KERN_INFO "No local APIC present\n");
908 return -1;
909 }
910
911 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
c70dcb74 912 boot_cpu_physical_apicid = 0;
1da177e4
LT
913 return 0;
914}
915
8643f9d0
YL
916void __init early_init_lapic_mapping(void)
917{
431ee79d 918 unsigned long phys_addr;
8643f9d0
YL
919
920 /*
921 * If no local APIC can be found then go out
922 * : it means there is no mpatable and MADT
923 */
924 if (!smp_found_config)
925 return;
926
431ee79d 927 phys_addr = mp_lapic_addr;
8643f9d0 928
431ee79d 929 set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
8643f9d0 930 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
431ee79d 931 APIC_BASE, phys_addr);
8643f9d0
YL
932
933 /*
934 * Fetch the APIC ID of the BSP in case we have a
935 * default configuration (or the MP table is broken).
936 */
05f2d12c 937 boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
8643f9d0
YL
938}
939
0e078e2f
TG
940/**
941 * init_apic_mappings - initialize APIC mappings
942 */
1da177e4
LT
943void __init init_apic_mappings(void)
944{
1da177e4
LT
945 /*
946 * If no local APIC can be found then set up a fake all
947 * zeroes page to simulate the local APIC and another
948 * one for the IO-APIC.
949 */
950 if (!smp_found_config && detect_init_APIC()) {
951 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
952 apic_phys = __pa(apic_phys);
953 } else
954 apic_phys = mp_lapic_addr;
955
956 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
7ffeeb1e
YL
957 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
958 APIC_BASE, apic_phys);
1da177e4
LT
959
960 /*
961 * Fetch the APIC ID of the BSP in case we have a
962 * default configuration (or the MP table is broken).
963 */
05f2d12c 964 boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
1da177e4
LT
965}
966
967/*
0e078e2f
TG
968 * This initializes the IO-APIC and APIC hardware if this is
969 * a UP kernel.
1da177e4 970 */
0e078e2f 971int __init APIC_init_uniprocessor(void)
1da177e4 972{
0e078e2f
TG
973 if (disable_apic) {
974 printk(KERN_INFO "Apic disabled\n");
975 return -1;
976 }
977 if (!cpu_has_apic) {
978 disable_apic = 1;
979 printk(KERN_INFO "Apic disabled by BIOS\n");
980 return -1;
981 }
1da177e4 982
0e078e2f 983 verify_local_APIC();
1da177e4 984
b5841765
GC
985 connect_bsp_APIC();
986
b6df1b8b 987 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
c70dcb74 988 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
1da177e4 989
0e078e2f 990 setup_local_APIC();
1da177e4 991
739f33b3
AK
992 /*
993 * Now enable IO-APICs, actually call clear_IO_APIC
994 * We need clear_IO_APIC before enabling vector on BP
995 */
996 if (!skip_ioapic_setup && nr_ioapics)
997 enable_IO_APIC();
998
acae7d90
MR
999 if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
1000 localise_nmi_watchdog();
739f33b3
AK
1001 end_local_APIC_setup();
1002
0e078e2f
TG
1003 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1004 setup_IO_APIC();
1005 else
1006 nr_ioapics = 0;
1007 setup_boot_APIC_clock();
1008 check_nmi_watchdog();
1009 return 0;
1da177e4
LT
1010}
1011
1012/*
0e078e2f 1013 * Local APIC interrupts
1da177e4
LT
1014 */
1015
0e078e2f
TG
1016/*
1017 * This interrupt should _never_ happen with our APIC/SMP architecture
1018 */
1019asmlinkage void smp_spurious_interrupt(void)
1da177e4 1020{
0e078e2f
TG
1021 unsigned int v;
1022 exit_idle();
1023 irq_enter();
1da177e4 1024 /*
0e078e2f
TG
1025 * Check if this really is a spurious interrupt and ACK it
1026 * if it is a vectored one. Just in case...
1027 * Spurious interrupts should not be ACKed.
1da177e4 1028 */
0e078e2f
TG
1029 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1030 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1031 ack_APIC_irq();
c4d58cbd 1032
0e078e2f
TG
1033 add_pda(irq_spurious_count, 1);
1034 irq_exit();
1035}
1da177e4 1036
0e078e2f
TG
1037/*
1038 * This interrupt should never happen with our APIC/SMP architecture
1039 */
1040asmlinkage void smp_error_interrupt(void)
1041{
1042 unsigned int v, v1;
1da177e4 1043
0e078e2f
TG
1044 exit_idle();
1045 irq_enter();
1046 /* First tickle the hardware, only then report what went on. -- REW */
1047 v = apic_read(APIC_ESR);
1048 apic_write(APIC_ESR, 0);
1049 v1 = apic_read(APIC_ESR);
1050 ack_APIC_irq();
1051 atomic_inc(&irq_err_count);
ba7eda4c 1052
0e078e2f
TG
1053 /* Here is what the APIC error bits mean:
1054 0: Send CS error
1055 1: Receive CS error
1056 2: Send accept error
1057 3: Receive accept error
1058 4: Reserved
1059 5: Send illegal vector
1060 6: Received illegal vector
1061 7: Illegal register address
1062 */
1063 printk(KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1064 smp_processor_id(), v , v1);
1065 irq_exit();
1da177e4
LT
1066}
1067
b5841765
GC
1068/**
1069 * * connect_bsp_APIC - attach the APIC to the interrupt system
1070 * */
1071void __init connect_bsp_APIC(void)
1072{
1073 enable_apic_mode();
1074}
1075
0e078e2f 1076void disconnect_bsp_APIC(int virt_wire_setup)
1da177e4 1077{
0e078e2f
TG
1078 /* Go back to Virtual Wire compatibility mode */
1079 unsigned long value;
1da177e4 1080
0e078e2f
TG
1081 /* For the spurious interrupt use vector F, and enable it */
1082 value = apic_read(APIC_SPIV);
1083 value &= ~APIC_VECTOR_MASK;
1084 value |= APIC_SPIV_APIC_ENABLED;
1085 value |= 0xf;
1086 apic_write(APIC_SPIV, value);
b8ce3359 1087
0e078e2f
TG
1088 if (!virt_wire_setup) {
1089 /*
1090 * For LVT0 make it edge triggered, active high,
1091 * external and enabled
1092 */
1093 value = apic_read(APIC_LVT0);
1094 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1095 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1096 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1097 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1098 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1099 apic_write(APIC_LVT0, value);
1100 } else {
1101 /* Disable LVT0 */
1102 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1103 }
b8ce3359 1104
0e078e2f
TG
1105 /* For LVT1 make it edge triggered, active high, nmi and enabled */
1106 value = apic_read(APIC_LVT1);
1107 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1108 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1109 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1110 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1111 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1112 apic_write(APIC_LVT1, value);
1da177e4
LT
1113}
1114
be8a5685
AS
1115void __cpuinit generic_processor_info(int apicid, int version)
1116{
1117 int cpu;
1118 cpumask_t tmp_map;
1119
1120 if (num_processors >= NR_CPUS) {
1121 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
1122 " Processor ignored.\n", NR_CPUS);
1123 return;
1124 }
1125
1126 if (num_processors >= maxcpus) {
1127 printk(KERN_WARNING "WARNING: maxcpus limit of %i reached."
1128 " Processor ignored.\n", maxcpus);
1129 return;
1130 }
1131
1132 num_processors++;
1133 cpus_complement(tmp_map, cpu_present_map);
1134 cpu = first_cpu(tmp_map);
1135
1136 physid_set(apicid, phys_cpu_present_map);
1137 if (apicid == boot_cpu_physical_apicid) {
1138 /*
1139 * x86_bios_cpu_apicid is required to have processors listed
1140 * in same order as logical cpu numbers. Hence the first
1141 * entry is BSP, and so on.
1142 */
1143 cpu = 0;
1144 }
e0da3364
YL
1145 if (apicid > max_physical_apicid)
1146 max_physical_apicid = apicid;
1147
be8a5685 1148 /* are we being called early in kernel startup? */
23ca4bba
MT
1149 if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
1150 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
1151 u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
be8a5685
AS
1152
1153 cpu_to_apicid[cpu] = apicid;
1154 bios_cpu_apicid[cpu] = apicid;
1155 } else {
1156 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1157 per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1158 }
1159
1160 cpu_set(cpu, cpu_possible_map);
1161 cpu_set(cpu, cpu_present_map);
1162}
1163
0c81c746
SS
1164int hard_smp_processor_id(void)
1165{
1166 return read_apic_id();
1167}
1168
89039b37 1169/*
0e078e2f 1170 * Power management
89039b37 1171 */
0e078e2f
TG
1172#ifdef CONFIG_PM
1173
1174static struct {
1175 /* 'active' is true if the local APIC was enabled by us and
1176 not the BIOS; this signifies that we are also responsible
1177 for disabling it before entering apm/acpi suspend */
1178 int active;
1179 /* r/w apic fields */
1180 unsigned int apic_id;
1181 unsigned int apic_taskpri;
1182 unsigned int apic_ldr;
1183 unsigned int apic_dfr;
1184 unsigned int apic_spiv;
1185 unsigned int apic_lvtt;
1186 unsigned int apic_lvtpc;
1187 unsigned int apic_lvt0;
1188 unsigned int apic_lvt1;
1189 unsigned int apic_lvterr;
1190 unsigned int apic_tmict;
1191 unsigned int apic_tdcr;
1192 unsigned int apic_thmr;
1193} apic_pm_state;
1194
1195static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1196{
1197 unsigned long flags;
1198 int maxlvt;
89039b37 1199
0e078e2f
TG
1200 if (!apic_pm_state.active)
1201 return 0;
89039b37 1202
0e078e2f 1203 maxlvt = lapic_get_maxlvt();
89039b37 1204
2d7a66d0 1205 apic_pm_state.apic_id = apic_read(APIC_ID);
0e078e2f
TG
1206 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1207 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1208 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1209 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1210 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1211 if (maxlvt >= 4)
1212 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1213 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1214 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1215 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1216 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1217 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1218#ifdef CONFIG_X86_MCE_INTEL
1219 if (maxlvt >= 5)
1220 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1221#endif
1222 local_irq_save(flags);
1223 disable_local_APIC();
1224 local_irq_restore(flags);
1225 return 0;
1da177e4
LT
1226}
1227
0e078e2f 1228static int lapic_resume(struct sys_device *dev)
1da177e4 1229{
0e078e2f
TG
1230 unsigned int l, h;
1231 unsigned long flags;
1232 int maxlvt;
1da177e4 1233
0e078e2f
TG
1234 if (!apic_pm_state.active)
1235 return 0;
89b831ef 1236
0e078e2f 1237 maxlvt = lapic_get_maxlvt();
1da177e4 1238
0e078e2f
TG
1239 local_irq_save(flags);
1240 rdmsr(MSR_IA32_APICBASE, l, h);
1241 l &= ~MSR_IA32_APICBASE_BASE;
1242 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1243 wrmsr(MSR_IA32_APICBASE, l, h);
1244 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1245 apic_write(APIC_ID, apic_pm_state.apic_id);
1246 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1247 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1248 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1249 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1250 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1251 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1252#ifdef CONFIG_X86_MCE_INTEL
1253 if (maxlvt >= 5)
1254 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1255#endif
1256 if (maxlvt >= 4)
1257 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1258 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1259 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1260 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1261 apic_write(APIC_ESR, 0);
1262 apic_read(APIC_ESR);
1263 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1264 apic_write(APIC_ESR, 0);
1265 apic_read(APIC_ESR);
1266 local_irq_restore(flags);
1267 return 0;
1268}
b8ce3359 1269
0e078e2f
TG
1270static struct sysdev_class lapic_sysclass = {
1271 .name = "lapic",
1272 .resume = lapic_resume,
1273 .suspend = lapic_suspend,
1274};
b8ce3359 1275
0e078e2f 1276static struct sys_device device_lapic = {
e83a5fdc
HS
1277 .id = 0,
1278 .cls = &lapic_sysclass,
0e078e2f 1279};
b8ce3359 1280
0e078e2f
TG
1281static void __cpuinit apic_pm_activate(void)
1282{
1283 apic_pm_state.active = 1;
1da177e4
LT
1284}
1285
0e078e2f 1286static int __init init_lapic_sysfs(void)
1da177e4 1287{
0e078e2f 1288 int error;
e83a5fdc 1289
0e078e2f
TG
1290 if (!cpu_has_apic)
1291 return 0;
1292 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
e83a5fdc 1293
0e078e2f
TG
1294 error = sysdev_class_register(&lapic_sysclass);
1295 if (!error)
1296 error = sysdev_register(&device_lapic);
1297 return error;
1da177e4 1298}
0e078e2f
TG
1299device_initcall(init_lapic_sysfs);
1300
1301#else /* CONFIG_PM */
1302
1303static void apic_pm_activate(void) { }
1304
1305#endif /* CONFIG_PM */
1da177e4
LT
1306
1307/*
f8bf3c65 1308 * apic_is_clustered_box() -- Check if we can expect good TSC
1da177e4
LT
1309 *
1310 * Thus far, the major user of this is IBM's Summit2 series:
1311 *
637029c6 1312 * Clustered boxes may have unsynced TSC problems if they are
1da177e4
LT
1313 * multi-chassis. Use available data to take a good guess.
1314 * If in doubt, go HPET.
1315 */
f8bf3c65 1316__cpuinit int apic_is_clustered_box(void)
1da177e4
LT
1317{
1318 int i, clusters, zeros;
1319 unsigned id;
322850af 1320 u16 *bios_cpu_apicid;
1da177e4
LT
1321 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1322
322850af
YL
1323 /*
1324 * there is not this kind of box with AMD CPU yet.
1325 * Some AMD box with quadcore cpu and 8 sockets apicid
1326 * will be [4, 0x23] or [8, 0x27] could be thought to
f8fffa45 1327 * vsmp box still need checking...
322850af 1328 */
1cb68487 1329 if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
322850af
YL
1330 return 0;
1331
23ca4bba 1332 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
376ec33f 1333 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
1da177e4
LT
1334
1335 for (i = 0; i < NR_CPUS; i++) {
e8c10ef9 1336 /* are we being called early in kernel startup? */
693e3c56
MT
1337 if (bios_cpu_apicid) {
1338 id = bios_cpu_apicid[i];
e8c10ef9 1339 }
1340 else if (i < nr_cpu_ids) {
1341 if (cpu_present(i))
1342 id = per_cpu(x86_bios_cpu_apicid, i);
1343 else
1344 continue;
1345 }
1346 else
1347 break;
1348
1da177e4
LT
1349 if (id != BAD_APICID)
1350 __set_bit(APIC_CLUSTERID(id), clustermap);
1351 }
1352
1353 /* Problem: Partially populated chassis may not have CPUs in some of
1354 * the APIC clusters they have been allocated. Only present CPUs have
602a54a8 1355 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
1356 * Since clusters are allocated sequentially, count zeros only if
1357 * they are bounded by ones.
1da177e4
LT
1358 */
1359 clusters = 0;
1360 zeros = 0;
1361 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1362 if (test_bit(i, clustermap)) {
1363 clusters += 1 + zeros;
1364 zeros = 0;
1365 } else
1366 ++zeros;
1367 }
1368
1cb68487
RT
1369 /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
1370 * not guaranteed to be synced between boards
1371 */
1372 if (is_vsmp_box() && clusters > 1)
1373 return 1;
1374
1da177e4 1375 /*
f8bf3c65 1376 * If clusters > 2, then should be multi-chassis.
1da177e4
LT
1377 * May have to revisit this when multi-core + hyperthreaded CPUs come
1378 * out, but AFAIK this will work even for them.
1379 */
1380 return (clusters > 2);
1381}
1382
1383/*
0e078e2f 1384 * APIC command line parameters
1da177e4 1385 */
0e078e2f 1386static int __init apic_set_verbosity(char *str)
1da177e4 1387{
0e078e2f
TG
1388 if (str == NULL) {
1389 skip_ioapic_setup = 0;
1390 ioapic_force = 1;
1391 return 0;
1da177e4 1392 }
0e078e2f
TG
1393 if (strcmp("debug", str) == 0)
1394 apic_verbosity = APIC_DEBUG;
1395 else if (strcmp("verbose", str) == 0)
1396 apic_verbosity = APIC_VERBOSE;
1397 else {
1398 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
1399 " use apic=verbose or apic=debug\n", str);
1400 return -EINVAL;
1da177e4
LT
1401 }
1402
1da177e4
LT
1403 return 0;
1404}
0e078e2f 1405early_param("apic", apic_set_verbosity);
1da177e4 1406
6935d1f9
TG
1407static __init int setup_disableapic(char *str)
1408{
1da177e4 1409 disable_apic = 1;
53756d37 1410 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
2c8c0e6b
AK
1411 return 0;
1412}
1413early_param("disableapic", setup_disableapic);
1da177e4 1414
2c8c0e6b 1415/* same as disableapic, for compatibility */
6935d1f9
TG
1416static __init int setup_nolapic(char *str)
1417{
2c8c0e6b 1418 return setup_disableapic(str);
6935d1f9 1419}
2c8c0e6b 1420early_param("nolapic", setup_nolapic);
1da177e4 1421
2e7c2838
LT
1422static int __init parse_lapic_timer_c2_ok(char *arg)
1423{
1424 local_apic_timer_c2_ok = 1;
1425 return 0;
1426}
1427early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1428
6935d1f9
TG
1429static __init int setup_noapictimer(char *str)
1430{
73dea47f 1431 if (str[0] != ' ' && str[0] != 0)
9b41046c 1432 return 0;
1da177e4 1433 disable_apic_timer = 1;
9b41046c 1434 return 1;
6935d1f9 1435}
9f75e9b7 1436__setup("noapictimer", setup_noapictimer);
73dea47f 1437
0c3749c4
AK
1438static __init int setup_apicpmtimer(char *s)
1439{
1440 apic_calibrate_pmtmr = 1;
7fd67843 1441 notsc_setup(NULL);
b8ce3359 1442 return 0;
0c3749c4
AK
1443}
1444__setup("apicpmtimer", setup_apicpmtimer);
1445
1e934dda
YL
1446static int __init lapic_insert_resource(void)
1447{
1448 if (!apic_phys)
1449 return -1;
1450
1451 /* Put local APIC into the resource map. */
1452 lapic_resource.start = apic_phys;
1453 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
1454 insert_resource(&iomem_resource, &lapic_resource);
1455
1456 return 0;
1457}
1458
1459/*
1460 * need call insert after e820_reserve_resources()
1461 * that is using request_resource
1462 */
1463late_initcall(lapic_insert_resource);
This page took 0.839639 seconds and 5 git commands to generate.