mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / arch / x86 / kernel / apic / apic.c
blob4abec38582092cc2142b4cad79264b341a0a3305
1 /*
2 * Local APIC handling, local APIC timers
4 * (c) 1999, 2000, 2009 Ingo Molnar <mingo@redhat.com>
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.
17 #include <linux/perf_event.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/mc146818rtc.h>
20 #include <linux/acpi_pmtmr.h>
21 #include <linux/clockchips.h>
22 #include <linux/interrupt.h>
23 #include <linux/bootmem.h>
24 #include <linux/ftrace.h>
25 #include <linux/ioport.h>
26 #include <linux/module.h>
27 #include <linux/syscore_ops.h>
28 #include <linux/delay.h>
29 #include <linux/timex.h>
30 #include <linux/i8253.h>
31 #include <linux/dmar.h>
32 #include <linux/init.h>
33 #include <linux/cpu.h>
34 #include <linux/dmi.h>
35 #include <linux/smp.h>
36 #include <linux/mm.h>
38 #include <asm/trace/irq_vectors.h>
39 #include <asm/irq_remapping.h>
40 #include <asm/perf_event.h>
41 #include <asm/x86_init.h>
42 #include <asm/pgalloc.h>
43 #include <linux/atomic.h>
44 #include <asm/mpspec.h>
45 #include <asm/i8259.h>
46 #include <asm/proto.h>
47 #include <asm/apic.h>
48 #include <asm/io_apic.h>
49 #include <asm/desc.h>
50 #include <asm/hpet.h>
51 #include <asm/idle.h>
52 #include <asm/mtrr.h>
53 #include <asm/time.h>
54 #include <asm/smp.h>
55 #include <asm/mce.h>
56 #include <asm/tsc.h>
57 #include <asm/hypervisor.h>
59 unsigned int num_processors;
61 unsigned disabled_cpus;
63 /* Processor that is doing the boot up */
64 unsigned int boot_cpu_physical_apicid = -1U;
67 * The highest APIC ID seen during enumeration.
69 unsigned int max_physical_apicid;
72 * Bitmask of physically existing CPUs:
74 physid_mask_t phys_cpu_present_map;
77 * Map cpu index to physical APIC ID
79 DEFINE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_cpu_to_apicid, BAD_APICID);
80 DEFINE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_bios_cpu_apicid, BAD_APICID);
81 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
82 EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
84 #ifdef CONFIG_X86_32
87 * On x86_32, the mapping between cpu and logical apicid may vary
88 * depending on apic in use. The following early percpu variable is
89 * used for the mapping. This is where the behaviors of x86_64 and 32
90 * actually diverge. Let's keep it ugly for now.
92 DEFINE_EARLY_PER_CPU_READ_MOSTLY(int, x86_cpu_to_logical_apicid, BAD_APICID);
94 /* Local APIC was disabled by the BIOS and enabled by the kernel */
95 static int enabled_via_apicbase;
98 * Handle interrupt mode configuration register (IMCR).
99 * This register controls whether the interrupt signals
100 * that reach the BSP come from the master PIC or from the
101 * local APIC. Before entering Symmetric I/O Mode, either
102 * the BIOS or the operating system must switch out of
103 * PIC Mode by changing the IMCR.
105 static inline void imcr_pic_to_apic(void)
107 /* select IMCR register */
108 outb(0x70, 0x22);
109 /* NMI and 8259 INTR go through APIC */
110 outb(0x01, 0x23);
113 static inline void imcr_apic_to_pic(void)
115 /* select IMCR register */
116 outb(0x70, 0x22);
117 /* NMI and 8259 INTR go directly to BSP */
118 outb(0x00, 0x23);
120 #endif
123 * Knob to control our willingness to enable the local APIC.
125 * +1=force-enable
127 static int force_enable_local_apic __initdata;
129 * APIC command line parameters
131 static int __init parse_lapic(char *arg)
133 if (config_enabled(CONFIG_X86_32) && !arg)
134 force_enable_local_apic = 1;
135 else if (arg && !strncmp(arg, "notscdeadline", 13))
136 setup_clear_cpu_cap(X86_FEATURE_TSC_DEADLINE_TIMER);
137 return 0;
139 early_param("lapic", parse_lapic);
141 #ifdef CONFIG_X86_64
142 static int apic_calibrate_pmtmr __initdata;
143 static __init int setup_apicpmtimer(char *s)
145 apic_calibrate_pmtmr = 1;
146 notsc_setup(NULL);
147 return 0;
149 __setup("apicpmtimer", setup_apicpmtimer);
150 #endif
152 int x2apic_mode;
153 #ifdef CONFIG_X86_X2APIC
154 /* x2apic enabled before OS handover */
155 int x2apic_preenabled;
156 static int x2apic_disabled;
157 static int nox2apic;
158 static __init int setup_nox2apic(char *str)
160 if (x2apic_enabled()) {
161 int apicid = native_apic_msr_read(APIC_ID);
163 if (apicid >= 255) {
164 pr_warning("Apicid: %08x, cannot enforce nox2apic\n",
165 apicid);
166 return 0;
169 pr_warning("x2apic already enabled. will disable it\n");
170 } else
171 setup_clear_cpu_cap(X86_FEATURE_X2APIC);
173 nox2apic = 1;
175 return 0;
177 early_param("nox2apic", setup_nox2apic);
178 #endif
180 unsigned long mp_lapic_addr;
181 int disable_apic;
182 /* Disable local APIC timer from the kernel commandline or via dmi quirk */
183 static int disable_apic_timer __initdata;
184 /* Local APIC timer works in C2 */
185 int local_apic_timer_c2_ok;
186 EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
188 int first_system_vector = 0xfe;
191 * Debug level, exported for io_apic.c
193 unsigned int apic_verbosity;
195 int pic_mode;
197 /* Have we found an MP table */
198 int smp_found_config;
200 static struct resource lapic_resource = {
201 .name = "Local APIC",
202 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
205 unsigned int lapic_timer_frequency = 0;
207 static void apic_pm_activate(void);
209 static unsigned long apic_phys;
212 * Get the LAPIC version
214 static inline int lapic_get_version(void)
216 return GET_APIC_VERSION(apic_read(APIC_LVR));
220 * Check, if the APIC is integrated or a separate chip
222 static inline int lapic_is_integrated(void)
224 #ifdef CONFIG_X86_64
225 return 1;
226 #else
227 return APIC_INTEGRATED(lapic_get_version());
228 #endif
232 * Check, whether this is a modern or a first generation APIC
234 static int modern_apic(void)
236 /* AMD systems use old APIC versions, so check the CPU */
237 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
238 boot_cpu_data.x86 >= 0xf)
239 return 1;
240 return lapic_get_version() >= 0x14;
244 * right after this call apic become NOOP driven
245 * so apic->write/read doesn't do anything
247 static void __init apic_disable(void)
249 pr_info("APIC: switched to apic NOOP\n");
250 apic = &apic_noop;
253 void native_apic_wait_icr_idle(void)
255 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
256 cpu_relax();
259 u32 native_safe_apic_wait_icr_idle(void)
261 u32 send_status;
262 int timeout;
264 timeout = 0;
265 do {
266 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
267 if (!send_status)
268 break;
269 inc_irq_stat(icr_read_retry_count);
270 udelay(100);
271 } while (timeout++ < 1000);
273 return send_status;
276 void native_apic_icr_write(u32 low, u32 id)
278 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
279 apic_write(APIC_ICR, low);
282 u64 native_apic_icr_read(void)
284 u32 icr1, icr2;
286 icr2 = apic_read(APIC_ICR2);
287 icr1 = apic_read(APIC_ICR);
289 return icr1 | ((u64)icr2 << 32);
292 #ifdef CONFIG_X86_32
294 * get_physical_broadcast - Get number of physical broadcast IDs
296 int get_physical_broadcast(void)
298 return modern_apic() ? 0xff : 0xf;
300 #endif
303 * lapic_get_maxlvt - get the maximum number of local vector table entries
305 int lapic_get_maxlvt(void)
307 unsigned int v;
309 v = apic_read(APIC_LVR);
311 * - we always have APIC integrated on 64bit mode
312 * - 82489DXs do not report # of LVT entries
314 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
318 * Local APIC timer
321 /* Clock divisor */
322 #define APIC_DIVISOR 16
323 #define TSC_DIVISOR 32
326 * This function sets up the local APIC timer, with a timeout of
327 * 'clocks' APIC bus clock. During calibration we actually call
328 * this function twice on the boot CPU, once with a bogus timeout
329 * value, second time for real. The other (noncalibrating) CPUs
330 * call this function only once, with the real, calibrated value.
332 * We do reads before writes even if unnecessary, to get around the
333 * P5 APIC double write bug.
335 static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
337 unsigned int lvtt_value, tmp_value;
339 lvtt_value = LOCAL_TIMER_VECTOR;
340 if (!oneshot)
341 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
342 else if (boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER))
343 lvtt_value |= APIC_LVT_TIMER_TSCDEADLINE;
345 if (!lapic_is_integrated())
346 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
348 if (!irqen)
349 lvtt_value |= APIC_LVT_MASKED;
351 apic_write(APIC_LVTT, lvtt_value);
353 if (lvtt_value & APIC_LVT_TIMER_TSCDEADLINE) {
355 * See Intel SDM: TSC-Deadline Mode chapter. In xAPIC mode,
356 * writing to the APIC LVTT and TSC_DEADLINE MSR isn't serialized.
357 * According to Intel, MFENCE can do the serialization here.
359 asm volatile("mfence" : : : "memory");
361 printk_once(KERN_DEBUG "TSC deadline timer enabled\n");
362 return;
366 * Divide PICLK by 16
368 tmp_value = apic_read(APIC_TDCR);
369 apic_write(APIC_TDCR,
370 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
371 APIC_TDR_DIV_16);
373 if (!oneshot)
374 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
378 * Setup extended LVT, AMD specific
380 * Software should use the LVT offsets the BIOS provides. The offsets
381 * are determined by the subsystems using it like those for MCE
382 * threshold or IBS. On K8 only offset 0 (APIC500) and MCE interrupts
383 * are supported. Beginning with family 10h at least 4 offsets are
384 * available.
386 * Since the offsets must be consistent for all cores, we keep track
387 * of the LVT offsets in software and reserve the offset for the same
388 * vector also to be used on other cores. An offset is freed by
389 * setting the entry to APIC_EILVT_MASKED.
391 * If the BIOS is right, there should be no conflicts. Otherwise a
392 * "[Firmware Bug]: ..." error message is generated. However, if
393 * software does not properly determines the offsets, it is not
394 * necessarily a BIOS bug.
397 static atomic_t eilvt_offsets[APIC_EILVT_NR_MAX];
399 static inline int eilvt_entry_is_changeable(unsigned int old, unsigned int new)
401 return (old & APIC_EILVT_MASKED)
402 || (new == APIC_EILVT_MASKED)
403 || ((new & ~APIC_EILVT_MASKED) == old);
406 static unsigned int reserve_eilvt_offset(int offset, unsigned int new)
408 unsigned int rsvd, vector;
410 if (offset >= APIC_EILVT_NR_MAX)
411 return ~0;
413 rsvd = atomic_read(&eilvt_offsets[offset]);
414 do {
415 vector = rsvd & ~APIC_EILVT_MASKED; /* 0: unassigned */
416 if (vector && !eilvt_entry_is_changeable(vector, new))
417 /* may not change if vectors are different */
418 return rsvd;
419 rsvd = atomic_cmpxchg(&eilvt_offsets[offset], rsvd, new);
420 } while (rsvd != new);
422 rsvd &= ~APIC_EILVT_MASKED;
423 if (rsvd && rsvd != vector)
424 pr_info("LVT offset %d assigned for vector 0x%02x\n",
425 offset, rsvd);
427 return new;
431 * If mask=1, the LVT entry does not generate interrupts while mask=0
432 * enables the vector. See also the BKDGs. Must be called with
433 * preemption disabled.
436 int setup_APIC_eilvt(u8 offset, u8 vector, u8 msg_type, u8 mask)
438 unsigned long reg = APIC_EILVTn(offset);
439 unsigned int new, old, reserved;
441 new = (mask << 16) | (msg_type << 8) | vector;
442 old = apic_read(reg);
443 reserved = reserve_eilvt_offset(offset, new);
445 if (reserved != new) {
446 pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
447 "vector 0x%x, but the register is already in use for "
448 "vector 0x%x on another cpu\n",
449 smp_processor_id(), reg, offset, new, reserved);
450 return -EINVAL;
453 if (!eilvt_entry_is_changeable(old, new)) {
454 pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
455 "vector 0x%x, but the register is already in use for "
456 "vector 0x%x on this cpu\n",
457 smp_processor_id(), reg, offset, new, old);
458 return -EBUSY;
461 apic_write(reg, new);
463 return 0;
465 EXPORT_SYMBOL_GPL(setup_APIC_eilvt);
468 * Program the next event, relative to now
470 static int lapic_next_event(unsigned long delta,
471 struct clock_event_device *evt)
473 apic_write(APIC_TMICT, delta);
474 return 0;
477 static int lapic_next_deadline(unsigned long delta,
478 struct clock_event_device *evt)
480 u64 tsc;
482 rdtscll(tsc);
483 wrmsrl(MSR_IA32_TSC_DEADLINE, tsc + (((u64) delta) * TSC_DIVISOR));
484 return 0;
488 * Setup the lapic timer in periodic or oneshot mode
490 static void lapic_timer_setup(enum clock_event_mode mode,
491 struct clock_event_device *evt)
493 unsigned long flags;
494 unsigned int v;
496 /* Lapic used as dummy for broadcast ? */
497 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
498 return;
500 local_irq_save(flags);
502 switch (mode) {
503 case CLOCK_EVT_MODE_PERIODIC:
504 case CLOCK_EVT_MODE_ONESHOT:
505 __setup_APIC_LVTT(lapic_timer_frequency,
506 mode != CLOCK_EVT_MODE_PERIODIC, 1);
507 break;
508 case CLOCK_EVT_MODE_UNUSED:
509 case CLOCK_EVT_MODE_SHUTDOWN:
510 v = apic_read(APIC_LVTT);
511 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
512 apic_write(APIC_LVTT, v);
513 apic_write(APIC_TMICT, 0);
514 break;
515 case CLOCK_EVT_MODE_RESUME:
516 /* Nothing to do here */
517 break;
520 local_irq_restore(flags);
524 * Local APIC timer broadcast function
526 static void lapic_timer_broadcast(const struct cpumask *mask)
528 #ifdef CONFIG_SMP
529 apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
530 #endif
535 * The local apic timer can be used for any function which is CPU local.
537 static struct clock_event_device lapic_clockevent = {
538 .name = "lapic",
539 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
540 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
541 .shift = 32,
542 .set_mode = lapic_timer_setup,
543 .set_next_event = lapic_next_event,
544 .broadcast = lapic_timer_broadcast,
545 .rating = 100,
546 .irq = -1,
548 static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
551 * Setup the local APIC timer for this CPU. Copy the initialized values
552 * of the boot CPU and register the clock event in the framework.
554 static void setup_APIC_timer(void)
556 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
558 if (this_cpu_has(X86_FEATURE_ARAT)) {
559 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_C3STOP;
560 /* Make LAPIC timer preferrable over percpu HPET */
561 lapic_clockevent.rating = 150;
564 memcpy(levt, &lapic_clockevent, sizeof(*levt));
565 levt->cpumask = cpumask_of(smp_processor_id());
567 if (this_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER)) {
568 levt->features &= ~(CLOCK_EVT_FEAT_PERIODIC |
569 CLOCK_EVT_FEAT_DUMMY);
570 levt->set_next_event = lapic_next_deadline;
571 clockevents_config_and_register(levt,
572 (tsc_khz / TSC_DIVISOR) * 1000,
573 0xF, ~0UL);
574 } else
575 clockevents_register_device(levt);
579 * In this functions we calibrate APIC bus clocks to the external timer.
581 * We want to do the calibration only once since we want to have local timer
582 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
583 * frequency.
585 * This was previously done by reading the PIT/HPET and waiting for a wrap
586 * around to find out, that a tick has elapsed. I have a box, where the PIT
587 * readout is broken, so it never gets out of the wait loop again. This was
588 * also reported by others.
590 * Monitoring the jiffies value is inaccurate and the clockevents
591 * infrastructure allows us to do a simple substitution of the interrupt
592 * handler.
594 * The calibration routine also uses the pm_timer when possible, as the PIT
595 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
596 * back to normal later in the boot process).
599 #define LAPIC_CAL_LOOPS (HZ/10)
601 static __initdata int lapic_cal_loops = -1;
602 static __initdata long lapic_cal_t1, lapic_cal_t2;
603 static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
604 static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
605 static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
608 * Temporary interrupt handler.
610 static void __init lapic_cal_handler(struct clock_event_device *dev)
612 unsigned long long tsc = 0;
613 long tapic = apic_read(APIC_TMCCT);
614 unsigned long pm = acpi_pm_read_early();
616 if (cpu_has_tsc)
617 rdtscll(tsc);
619 switch (lapic_cal_loops++) {
620 case 0:
621 lapic_cal_t1 = tapic;
622 lapic_cal_tsc1 = tsc;
623 lapic_cal_pm1 = pm;
624 lapic_cal_j1 = jiffies;
625 break;
627 case LAPIC_CAL_LOOPS:
628 lapic_cal_t2 = tapic;
629 lapic_cal_tsc2 = tsc;
630 if (pm < lapic_cal_pm1)
631 pm += ACPI_PM_OVRRUN;
632 lapic_cal_pm2 = pm;
633 lapic_cal_j2 = jiffies;
634 break;
638 static int __init
639 calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc)
641 const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
642 const long pm_thresh = pm_100ms / 100;
643 unsigned long mult;
644 u64 res;
646 #ifndef CONFIG_X86_PM_TIMER
647 return -1;
648 #endif
650 apic_printk(APIC_VERBOSE, "... PM-Timer delta = %ld\n", deltapm);
652 /* Check, if the PM timer is available */
653 if (!deltapm)
654 return -1;
656 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
658 if (deltapm > (pm_100ms - pm_thresh) &&
659 deltapm < (pm_100ms + pm_thresh)) {
660 apic_printk(APIC_VERBOSE, "... PM-Timer result ok\n");
661 return 0;
664 res = (((u64)deltapm) * mult) >> 22;
665 do_div(res, 1000000);
666 pr_warning("APIC calibration not consistent "
667 "with PM-Timer: %ldms instead of 100ms\n",(long)res);
669 /* Correct the lapic counter value */
670 res = (((u64)(*delta)) * pm_100ms);
671 do_div(res, deltapm);
672 pr_info("APIC delta adjusted to PM-Timer: "
673 "%lu (%ld)\n", (unsigned long)res, *delta);
674 *delta = (long)res;
676 /* Correct the tsc counter value */
677 if (cpu_has_tsc) {
678 res = (((u64)(*deltatsc)) * pm_100ms);
679 do_div(res, deltapm);
680 apic_printk(APIC_VERBOSE, "TSC delta adjusted to "
681 "PM-Timer: %lu (%ld)\n",
682 (unsigned long)res, *deltatsc);
683 *deltatsc = (long)res;
686 return 0;
689 static int __init calibrate_APIC_clock(void)
691 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
692 void (*real_handler)(struct clock_event_device *dev);
693 unsigned long deltaj;
694 long delta, deltatsc;
695 int pm_referenced = 0;
698 * check if lapic timer has already been calibrated by platform
699 * specific routine, such as tsc calibration code. if so, we just fill
700 * in the clockevent structure and return.
703 if (boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER)) {
704 return 0;
705 } else if (lapic_timer_frequency) {
706 apic_printk(APIC_VERBOSE, "lapic timer already calibrated %d\n",
707 lapic_timer_frequency);
708 lapic_clockevent.mult = div_sc(lapic_timer_frequency/APIC_DIVISOR,
709 TICK_NSEC, lapic_clockevent.shift);
710 lapic_clockevent.max_delta_ns =
711 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
712 lapic_clockevent.min_delta_ns =
713 clockevent_delta2ns(0xF, &lapic_clockevent);
714 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
715 return 0;
718 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
719 "calibrating APIC timer ...\n");
721 local_irq_disable();
723 /* Replace the global interrupt handler */
724 real_handler = global_clock_event->event_handler;
725 global_clock_event->event_handler = lapic_cal_handler;
728 * Setup the APIC counter to maximum. There is no way the lapic
729 * can underflow in the 100ms detection time frame
731 __setup_APIC_LVTT(0xffffffff, 0, 0);
733 /* Let the interrupts run */
734 local_irq_enable();
736 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
737 cpu_relax();
739 local_irq_disable();
741 /* Restore the real event handler */
742 global_clock_event->event_handler = real_handler;
744 /* Build delta t1-t2 as apic timer counts down */
745 delta = lapic_cal_t1 - lapic_cal_t2;
746 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
748 deltatsc = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
750 /* we trust the PM based calibration if possible */
751 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
752 &delta, &deltatsc);
754 /* Calculate the scaled math multiplication factor */
755 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
756 lapic_clockevent.shift);
757 lapic_clockevent.max_delta_ns =
758 clockevent_delta2ns(0x7FFFFFFF, &lapic_clockevent);
759 lapic_clockevent.min_delta_ns =
760 clockevent_delta2ns(0xF, &lapic_clockevent);
762 lapic_timer_frequency = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
764 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
765 apic_printk(APIC_VERBOSE, "..... mult: %u\n", lapic_clockevent.mult);
766 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
767 lapic_timer_frequency);
769 if (cpu_has_tsc) {
770 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
771 "%ld.%04ld MHz.\n",
772 (deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
773 (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
776 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
777 "%u.%04u MHz.\n",
778 lapic_timer_frequency / (1000000 / HZ),
779 lapic_timer_frequency % (1000000 / HZ));
782 * Do a sanity check on the APIC calibration result
784 if (lapic_timer_frequency < (1000000 / HZ)) {
785 local_irq_enable();
786 pr_warning("APIC frequency too slow, disabling apic timer\n");
787 return -1;
790 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
793 * PM timer calibration failed or not turned on
794 * so lets try APIC timer based calibration
796 if (!pm_referenced) {
797 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
800 * Setup the apic timer manually
802 levt->event_handler = lapic_cal_handler;
803 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
804 lapic_cal_loops = -1;
806 /* Let the interrupts run */
807 local_irq_enable();
809 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
810 cpu_relax();
812 /* Stop the lapic timer */
813 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
815 /* Jiffies delta */
816 deltaj = lapic_cal_j2 - lapic_cal_j1;
817 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
819 /* Check, if the jiffies result is consistent */
820 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
821 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
822 else
823 levt->features |= CLOCK_EVT_FEAT_DUMMY;
824 } else
825 local_irq_enable();
827 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
828 pr_warning("APIC timer disabled due to verification failure\n");
829 return -1;
832 return 0;
836 * Setup the boot APIC
838 * Calibrate and verify the result.
840 void __init setup_boot_APIC_clock(void)
843 * The local apic timer can be disabled via the kernel
844 * commandline or from the CPU detection code. Register the lapic
845 * timer as a dummy clock event source on SMP systems, so the
846 * broadcast mechanism is used. On UP systems simply ignore it.
848 if (disable_apic_timer) {
849 pr_info("Disabling APIC timer\n");
850 /* No broadcast on UP ! */
851 if (num_possible_cpus() > 1) {
852 lapic_clockevent.mult = 1;
853 setup_APIC_timer();
855 return;
858 if (calibrate_APIC_clock()) {
859 /* No broadcast on UP ! */
860 if (num_possible_cpus() > 1)
861 setup_APIC_timer();
862 return;
866 * If nmi_watchdog is set to IO_APIC, we need the
867 * PIT/HPET going. Otherwise register lapic as a dummy
868 * device.
870 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
872 /* Setup the lapic or request the broadcast */
873 setup_APIC_timer();
876 void setup_secondary_APIC_clock(void)
878 setup_APIC_timer();
882 * The guts of the apic timer interrupt
884 static void local_apic_timer_interrupt(void)
886 int cpu = smp_processor_id();
887 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
890 * Normally we should not be here till LAPIC has been initialized but
891 * in some cases like kdump, its possible that there is a pending LAPIC
892 * timer interrupt from previous kernel's context and is delivered in
893 * new kernel the moment interrupts are enabled.
895 * Interrupts are enabled early and LAPIC is setup much later, hence
896 * its possible that when we get here evt->event_handler is NULL.
897 * Check for event_handler being NULL and discard the interrupt as
898 * spurious.
900 if (!evt->event_handler) {
901 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
902 /* Switch it off */
903 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
904 return;
908 * the NMI deadlock-detector uses this.
910 inc_irq_stat(apic_timer_irqs);
912 evt->event_handler(evt);
916 * Local APIC timer interrupt. This is the most natural way for doing
917 * local interrupts, but local timer interrupts can be emulated by
918 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
920 * [ if a single-CPU system runs an SMP kernel then we call the local
921 * interrupt as well. Thus we cannot inline the local irq ... ]
923 __visible void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
925 struct pt_regs *old_regs = set_irq_regs(regs);
928 * NOTE! We'd better ACK the irq immediately,
929 * because timer handling can be slow.
931 * update_process_times() expects us to have done irq_enter().
932 * Besides, if we don't timer interrupts ignore the global
933 * interrupt lock, which is the WrongThing (tm) to do.
935 entering_ack_irq();
936 local_apic_timer_interrupt();
937 exiting_irq();
939 set_irq_regs(old_regs);
942 __visible void __irq_entry smp_trace_apic_timer_interrupt(struct pt_regs *regs)
944 struct pt_regs *old_regs = set_irq_regs(regs);
947 * NOTE! We'd better ACK the irq immediately,
948 * because timer handling can be slow.
950 * update_process_times() expects us to have done irq_enter().
951 * Besides, if we don't timer interrupts ignore the global
952 * interrupt lock, which is the WrongThing (tm) to do.
954 entering_ack_irq();
955 trace_local_timer_entry(LOCAL_TIMER_VECTOR);
956 local_apic_timer_interrupt();
957 trace_local_timer_exit(LOCAL_TIMER_VECTOR);
958 exiting_irq();
960 set_irq_regs(old_regs);
963 int setup_profiling_timer(unsigned int multiplier)
965 return -EINVAL;
969 * Local APIC start and shutdown
973 * clear_local_APIC - shutdown the local APIC
975 * This is called, when a CPU is disabled and before rebooting, so the state of
976 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
977 * leftovers during boot.
979 void clear_local_APIC(void)
981 int maxlvt;
982 u32 v;
984 /* APIC hasn't been mapped yet */
985 if (!x2apic_mode && !apic_phys)
986 return;
988 maxlvt = lapic_get_maxlvt();
990 * Masking an LVT entry can trigger a local APIC error
991 * if the vector is zero. Mask LVTERR first to prevent this.
993 if (maxlvt >= 3) {
994 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
995 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
998 * Careful: we have to set masks only first to deassert
999 * any level-triggered sources.
1001 v = apic_read(APIC_LVTT);
1002 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
1003 v = apic_read(APIC_LVT0);
1004 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1005 v = apic_read(APIC_LVT1);
1006 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
1007 if (maxlvt >= 4) {
1008 v = apic_read(APIC_LVTPC);
1009 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
1012 /* lets not touch this if we didn't frob it */
1013 #ifdef CONFIG_X86_THERMAL_VECTOR
1014 if (maxlvt >= 5) {
1015 v = apic_read(APIC_LVTTHMR);
1016 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
1018 #endif
1019 #ifdef CONFIG_X86_MCE_INTEL
1020 if (maxlvt >= 6) {
1021 v = apic_read(APIC_LVTCMCI);
1022 if (!(v & APIC_LVT_MASKED))
1023 apic_write(APIC_LVTCMCI, v | APIC_LVT_MASKED);
1025 #endif
1028 * Clean APIC state for other OSs:
1030 apic_write(APIC_LVTT, APIC_LVT_MASKED);
1031 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1032 apic_write(APIC_LVT1, APIC_LVT_MASKED);
1033 if (maxlvt >= 3)
1034 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
1035 if (maxlvt >= 4)
1036 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
1038 /* Integrated APIC (!82489DX) ? */
1039 if (lapic_is_integrated()) {
1040 if (maxlvt > 3)
1041 /* Clear ESR due to Pentium errata 3AP and 11AP */
1042 apic_write(APIC_ESR, 0);
1043 apic_read(APIC_ESR);
1048 * disable_local_APIC - clear and disable the local APIC
1050 void disable_local_APIC(void)
1052 unsigned int value;
1054 /* APIC hasn't been mapped yet */
1055 if (!x2apic_mode && !apic_phys)
1056 return;
1058 clear_local_APIC();
1061 * Disable APIC (implies clearing of registers
1062 * for 82489DX!).
1064 value = apic_read(APIC_SPIV);
1065 value &= ~APIC_SPIV_APIC_ENABLED;
1066 apic_write(APIC_SPIV, value);
1068 #ifdef CONFIG_X86_32
1070 * When LAPIC was disabled by the BIOS and enabled by the kernel,
1071 * restore the disabled state.
1073 if (enabled_via_apicbase) {
1074 unsigned int l, h;
1076 rdmsr(MSR_IA32_APICBASE, l, h);
1077 l &= ~MSR_IA32_APICBASE_ENABLE;
1078 wrmsr(MSR_IA32_APICBASE, l, h);
1080 #endif
1084 * If Linux enabled the LAPIC against the BIOS default disable it down before
1085 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
1086 * not power-off. Additionally clear all LVT entries before disable_local_APIC
1087 * for the case where Linux didn't enable the LAPIC.
1089 void lapic_shutdown(void)
1091 unsigned long flags;
1093 if (!cpu_has_apic && !apic_from_smp_config())
1094 return;
1096 local_irq_save(flags);
1098 #ifdef CONFIG_X86_32
1099 if (!enabled_via_apicbase)
1100 clear_local_APIC();
1101 else
1102 #endif
1103 disable_local_APIC();
1106 local_irq_restore(flags);
1110 * This is to verify that we're looking at a real local APIC.
1111 * Check these against your board if the CPUs aren't getting
1112 * started for no apparent reason.
1114 int __init verify_local_APIC(void)
1116 unsigned int reg0, reg1;
1119 * The version register is read-only in a real APIC.
1121 reg0 = apic_read(APIC_LVR);
1122 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
1123 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
1124 reg1 = apic_read(APIC_LVR);
1125 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
1128 * The two version reads above should print the same
1129 * numbers. If the second one is different, then we
1130 * poke at a non-APIC.
1132 if (reg1 != reg0)
1133 return 0;
1136 * Check if the version looks reasonably.
1138 reg1 = GET_APIC_VERSION(reg0);
1139 if (reg1 == 0x00 || reg1 == 0xff)
1140 return 0;
1141 reg1 = lapic_get_maxlvt();
1142 if (reg1 < 0x02 || reg1 == 0xff)
1143 return 0;
1146 * The ID register is read/write in a real APIC.
1148 reg0 = apic_read(APIC_ID);
1149 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
1150 apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
1151 reg1 = apic_read(APIC_ID);
1152 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
1153 apic_write(APIC_ID, reg0);
1154 if (reg1 != (reg0 ^ apic->apic_id_mask))
1155 return 0;
1158 * The next two are just to see if we have sane values.
1159 * They're only really relevant if we're in Virtual Wire
1160 * compatibility mode, but most boxes are anymore.
1162 reg0 = apic_read(APIC_LVT0);
1163 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
1164 reg1 = apic_read(APIC_LVT1);
1165 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
1167 return 1;
1171 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1173 void __init sync_Arb_IDs(void)
1176 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1177 * needed on AMD.
1179 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1180 return;
1183 * Wait for idle.
1185 apic_wait_icr_idle();
1187 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
1188 apic_write(APIC_ICR, APIC_DEST_ALLINC |
1189 APIC_INT_LEVELTRIG | APIC_DM_INIT);
1193 * An initial setup of the virtual wire mode.
1195 void __init init_bsp_APIC(void)
1197 unsigned int value;
1200 * Don't do the setup now if we have a SMP BIOS as the
1201 * through-I/O-APIC virtual wire mode might be active.
1203 if (smp_found_config || !cpu_has_apic)
1204 return;
1207 * Do not trust the local APIC being empty at bootup.
1209 clear_local_APIC();
1212 * Enable APIC.
1214 value = apic_read(APIC_SPIV);
1215 value &= ~APIC_VECTOR_MASK;
1216 value |= APIC_SPIV_APIC_ENABLED;
1218 #ifdef CONFIG_X86_32
1219 /* This bit is reserved on P4/Xeon and should be cleared */
1220 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1221 (boot_cpu_data.x86 == 15))
1222 value &= ~APIC_SPIV_FOCUS_DISABLED;
1223 else
1224 #endif
1225 value |= APIC_SPIV_FOCUS_DISABLED;
1226 value |= SPURIOUS_APIC_VECTOR;
1227 apic_write(APIC_SPIV, value);
1230 * Set up the virtual wire mode.
1232 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1233 value = APIC_DM_NMI;
1234 if (!lapic_is_integrated()) /* 82489DX */
1235 value |= APIC_LVT_LEVEL_TRIGGER;
1236 apic_write(APIC_LVT1, value);
1239 static void lapic_setup_esr(void)
1241 unsigned int oldvalue, value, maxlvt;
1243 if (!lapic_is_integrated()) {
1244 pr_info("No ESR for 82489DX.\n");
1245 return;
1248 if (apic->disable_esr) {
1250 * Something untraceable is creating bad interrupts on
1251 * secondary quads ... for the moment, just leave the
1252 * ESR disabled - we can't do anything useful with the
1253 * errors anyway - mbligh
1255 pr_info("Leaving ESR disabled.\n");
1256 return;
1259 maxlvt = lapic_get_maxlvt();
1260 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1261 apic_write(APIC_ESR, 0);
1262 oldvalue = apic_read(APIC_ESR);
1264 /* enables sending errors */
1265 value = ERROR_APIC_VECTOR;
1266 apic_write(APIC_LVTERR, value);
1269 * spec says clear errors after enabling vector.
1271 if (maxlvt > 3)
1272 apic_write(APIC_ESR, 0);
1273 value = apic_read(APIC_ESR);
1274 if (value != oldvalue)
1275 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1276 "vector: 0x%08x after: 0x%08x\n",
1277 oldvalue, value);
1281 * setup_local_APIC - setup the local APIC
1283 * Used to setup local APIC while initializing BSP or bringin up APs.
1284 * Always called with preemption disabled.
1286 void setup_local_APIC(void)
1288 int cpu = smp_processor_id();
1289 unsigned int value, queued;
1290 int i, j, acked = 0;
1291 unsigned long long tsc = 0, ntsc;
1292 long long max_loops = cpu_khz ? cpu_khz : 1000000;
1294 if (cpu_has_tsc)
1295 rdtscll(tsc);
1297 if (disable_apic) {
1298 disable_ioapic_support();
1299 return;
1302 #ifdef CONFIG_X86_32
1303 /* Pound the ESR really hard over the head with a big hammer - mbligh */
1304 if (lapic_is_integrated() && apic->disable_esr) {
1305 apic_write(APIC_ESR, 0);
1306 apic_write(APIC_ESR, 0);
1307 apic_write(APIC_ESR, 0);
1308 apic_write(APIC_ESR, 0);
1310 #endif
1311 perf_events_lapic_init();
1314 * Double-check whether this APIC is really registered.
1315 * This is meaningless in clustered apic mode, so we skip it.
1317 BUG_ON(!apic->apic_id_registered());
1320 * Intel recommends to set DFR, LDR and TPR before enabling
1321 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1322 * document number 292116). So here it goes...
1324 apic->init_apic_ldr();
1326 #ifdef CONFIG_X86_32
1328 * APIC LDR is initialized. If logical_apicid mapping was
1329 * initialized during get_smp_config(), make sure it matches the
1330 * actual value.
1332 i = early_per_cpu(x86_cpu_to_logical_apicid, cpu);
1333 WARN_ON(i != BAD_APICID && i != logical_smp_processor_id());
1334 /* always use the value from LDR */
1335 early_per_cpu(x86_cpu_to_logical_apicid, cpu) =
1336 logical_smp_processor_id();
1339 * Some NUMA implementations (NUMAQ) don't initialize apicid to
1340 * node mapping during NUMA init. Now that logical apicid is
1341 * guaranteed to be known, give it another chance. This is already
1342 * a bit too late - percpu allocation has already happened without
1343 * proper NUMA affinity.
1345 if (apic->x86_32_numa_cpu_node)
1346 set_apicid_to_node(early_per_cpu(x86_cpu_to_apicid, cpu),
1347 apic->x86_32_numa_cpu_node(cpu));
1348 #endif
1351 * Set Task Priority to 'accept all'. We never change this
1352 * later on.
1354 value = apic_read(APIC_TASKPRI);
1355 value &= ~APIC_TPRI_MASK;
1356 apic_write(APIC_TASKPRI, value);
1359 * After a crash, we no longer service the interrupts and a pending
1360 * interrupt from previous kernel might still have ISR bit set.
1362 * Most probably by now CPU has serviced that pending interrupt and
1363 * it might not have done the ack_APIC_irq() because it thought,
1364 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1365 * does not clear the ISR bit and cpu thinks it has already serivced
1366 * the interrupt. Hence a vector might get locked. It was noticed
1367 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1369 do {
1370 queued = 0;
1371 for (i = APIC_ISR_NR - 1; i >= 0; i--)
1372 queued |= apic_read(APIC_IRR + i*0x10);
1374 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1375 value = apic_read(APIC_ISR + i*0x10);
1376 for (j = 31; j >= 0; j--) {
1377 if (value & (1<<j)) {
1378 ack_APIC_irq();
1379 acked++;
1383 if (acked > 256) {
1384 printk(KERN_ERR "LAPIC pending interrupts after %d EOI\n",
1385 acked);
1386 break;
1388 if (queued) {
1389 if (cpu_has_tsc && cpu_khz) {
1390 rdtscll(ntsc);
1391 max_loops = (cpu_khz << 10) - (ntsc - tsc);
1392 } else
1393 max_loops--;
1395 } while (queued && max_loops > 0);
1396 WARN_ON(max_loops <= 0);
1399 * Now that we are all set up, enable the APIC
1401 value = apic_read(APIC_SPIV);
1402 value &= ~APIC_VECTOR_MASK;
1404 * Enable APIC
1406 value |= APIC_SPIV_APIC_ENABLED;
1408 #ifdef CONFIG_X86_32
1410 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1411 * certain networking cards. If high frequency interrupts are
1412 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1413 * entry is masked/unmasked at a high rate as well then sooner or
1414 * later IOAPIC line gets 'stuck', no more interrupts are received
1415 * from the device. If focus CPU is disabled then the hang goes
1416 * away, oh well :-(
1418 * [ This bug can be reproduced easily with a level-triggered
1419 * PCI Ne2000 networking cards and PII/PIII processors, dual
1420 * BX chipset. ]
1423 * Actually disabling the focus CPU check just makes the hang less
1424 * frequent as it makes the interrupt distributon model be more
1425 * like LRU than MRU (the short-term load is more even across CPUs).
1426 * See also the comment in end_level_ioapic_irq(). --macro
1430 * - enable focus processor (bit==0)
1431 * - 64bit mode always use processor focus
1432 * so no need to set it
1434 value &= ~APIC_SPIV_FOCUS_DISABLED;
1435 #endif
1438 * Set spurious IRQ vector
1440 value |= SPURIOUS_APIC_VECTOR;
1441 apic_write(APIC_SPIV, value);
1444 * Set up LVT0, LVT1:
1446 * set up through-local-APIC on the BP's LINT0. This is not
1447 * strictly necessary in pure symmetric-IO mode, but sometimes
1448 * we delegate interrupts to the 8259A.
1451 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1453 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1454 if (!cpu && (pic_mode || !value)) {
1455 value = APIC_DM_EXTINT;
1456 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", cpu);
1457 } else {
1458 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1459 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", cpu);
1461 apic_write(APIC_LVT0, value);
1464 * only the BP should see the LINT1 NMI signal, obviously.
1466 if (!cpu)
1467 value = APIC_DM_NMI;
1468 else
1469 value = APIC_DM_NMI | APIC_LVT_MASKED;
1470 if (!lapic_is_integrated()) /* 82489DX */
1471 value |= APIC_LVT_LEVEL_TRIGGER;
1472 apic_write(APIC_LVT1, value);
1474 #ifdef CONFIG_X86_MCE_INTEL
1475 /* Recheck CMCI information after local APIC is up on CPU #0 */
1476 if (!cpu)
1477 cmci_recheck();
1478 #endif
1481 void end_local_APIC_setup(void)
1483 lapic_setup_esr();
1485 #ifdef CONFIG_X86_32
1487 unsigned int value;
1488 /* Disable the local apic timer */
1489 value = apic_read(APIC_LVTT);
1490 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1491 apic_write(APIC_LVTT, value);
1493 #endif
1495 apic_pm_activate();
1498 void __init bsp_end_local_APIC_setup(void)
1500 end_local_APIC_setup();
1503 * Now that local APIC setup is completed for BP, configure the fault
1504 * handling for interrupt remapping.
1506 irq_remap_enable_fault_handling();
1510 #ifdef CONFIG_X86_X2APIC
1512 * Need to disable xapic and x2apic at the same time and then enable xapic mode
1514 static inline void __disable_x2apic(u64 msr)
1516 wrmsrl(MSR_IA32_APICBASE,
1517 msr & ~(X2APIC_ENABLE | XAPIC_ENABLE));
1518 wrmsrl(MSR_IA32_APICBASE, msr & ~X2APIC_ENABLE);
1521 static __init void disable_x2apic(void)
1523 u64 msr;
1525 if (!cpu_has_x2apic)
1526 return;
1528 rdmsrl(MSR_IA32_APICBASE, msr);
1529 if (msr & X2APIC_ENABLE) {
1530 u32 x2apic_id = read_apic_id();
1532 if (x2apic_id >= 255)
1533 panic("Cannot disable x2apic, id: %08x\n", x2apic_id);
1535 pr_info("Disabling x2apic\n");
1536 __disable_x2apic(msr);
1538 if (nox2apic) {
1539 clear_cpu_cap(&cpu_data(0), X86_FEATURE_X2APIC);
1540 setup_clear_cpu_cap(X86_FEATURE_X2APIC);
1543 x2apic_disabled = 1;
1544 x2apic_mode = 0;
1546 register_lapic_address(mp_lapic_addr);
1550 void check_x2apic(void)
1552 if (x2apic_enabled()) {
1553 pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
1554 x2apic_preenabled = x2apic_mode = 1;
1558 void enable_x2apic(void)
1560 u64 msr;
1562 rdmsrl(MSR_IA32_APICBASE, msr);
1563 if (x2apic_disabled) {
1564 __disable_x2apic(msr);
1565 return;
1568 if (!x2apic_mode)
1569 return;
1571 if (!(msr & X2APIC_ENABLE)) {
1572 printk_once(KERN_INFO "Enabling x2apic\n");
1573 wrmsrl(MSR_IA32_APICBASE, msr | X2APIC_ENABLE);
1576 #endif /* CONFIG_X86_X2APIC */
1578 int __init enable_IR(void)
1580 #ifdef CONFIG_IRQ_REMAP
1581 if (!irq_remapping_supported()) {
1582 pr_debug("intr-remapping not supported\n");
1583 return -1;
1586 if (!x2apic_preenabled && skip_ioapic_setup) {
1587 pr_info("Skipped enabling intr-remap because of skipping "
1588 "io-apic setup\n");
1589 return -1;
1592 return irq_remapping_enable();
1593 #endif
1594 return -1;
1597 void __init enable_IR_x2apic(void)
1599 unsigned long flags;
1600 int ret, x2apic_enabled = 0;
1601 int hardware_init_ret;
1603 if (skip_ioapic_setup)
1604 return;
1606 /* Make sure irq_remap_ops are initialized */
1607 setup_irq_remapping_ops();
1609 hardware_init_ret = irq_remapping_prepare();
1610 if (hardware_init_ret && !x2apic_supported())
1611 return;
1613 ret = save_ioapic_entries();
1614 if (ret) {
1615 pr_info("Saving IO-APIC state failed: %d\n", ret);
1616 return;
1619 local_irq_save(flags);
1620 legacy_pic->mask_all();
1621 mask_ioapic_entries();
1623 if (x2apic_preenabled && nox2apic)
1624 disable_x2apic();
1626 if (hardware_init_ret)
1627 ret = -1;
1628 else
1629 ret = enable_IR();
1631 if (!x2apic_supported())
1632 goto skip_x2apic;
1634 if (ret < 0) {
1635 /* IR is required if there is APIC ID > 255 even when running
1636 * under KVM
1638 if (max_physical_apicid > 255 ||
1639 !hypervisor_x2apic_available()) {
1640 if (x2apic_preenabled)
1641 disable_x2apic();
1642 goto skip_x2apic;
1645 * without IR all CPUs can be addressed by IOAPIC/MSI
1646 * only in physical mode
1648 x2apic_force_phys();
1651 if (ret == IRQ_REMAP_XAPIC_MODE) {
1652 pr_info("x2apic not enabled, IRQ remapping is in xapic mode\n");
1653 goto skip_x2apic;
1656 x2apic_enabled = 1;
1658 if (x2apic_supported() && !x2apic_mode) {
1659 x2apic_mode = 1;
1660 enable_x2apic();
1661 pr_info("Enabled x2apic\n");
1664 skip_x2apic:
1665 if (ret < 0) /* IR enabling failed */
1666 restore_ioapic_entries();
1667 legacy_pic->restore_mask();
1668 local_irq_restore(flags);
1671 #ifdef CONFIG_X86_64
1673 * Detect and enable local APICs on non-SMP boards.
1674 * Original code written by Keir Fraser.
1675 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1676 * not correctly set up (usually the APIC timer won't work etc.)
1678 static int __init detect_init_APIC(void)
1680 if (!cpu_has_apic) {
1681 pr_info("No local APIC present\n");
1682 return -1;
1685 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1686 return 0;
1688 #else
1690 static int __init apic_verify(void)
1692 u32 features, h, l;
1695 * The APIC feature bit should now be enabled
1696 * in `cpuid'
1698 features = cpuid_edx(1);
1699 if (!(features & (1 << X86_FEATURE_APIC))) {
1700 pr_warning("Could not enable APIC!\n");
1701 return -1;
1703 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1704 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1706 /* The BIOS may have set up the APIC at some other address */
1707 if (boot_cpu_data.x86 >= 6) {
1708 rdmsr(MSR_IA32_APICBASE, l, h);
1709 if (l & MSR_IA32_APICBASE_ENABLE)
1710 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1713 pr_info("Found and enabled local APIC!\n");
1714 return 0;
1717 int __init apic_force_enable(unsigned long addr)
1719 u32 h, l;
1721 if (disable_apic)
1722 return -1;
1725 * Some BIOSes disable the local APIC in the APIC_BASE
1726 * MSR. This can only be done in software for Intel P6 or later
1727 * and AMD K7 (Model > 1) or later.
1729 if (boot_cpu_data.x86 >= 6) {
1730 rdmsr(MSR_IA32_APICBASE, l, h);
1731 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1732 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
1733 l &= ~MSR_IA32_APICBASE_BASE;
1734 l |= MSR_IA32_APICBASE_ENABLE | addr;
1735 wrmsr(MSR_IA32_APICBASE, l, h);
1736 enabled_via_apicbase = 1;
1739 return apic_verify();
1743 * Detect and initialize APIC
1745 static int __init detect_init_APIC(void)
1747 /* Disabled by kernel option? */
1748 if (disable_apic)
1749 return -1;
1751 switch (boot_cpu_data.x86_vendor) {
1752 case X86_VENDOR_AMD:
1753 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1754 (boot_cpu_data.x86 >= 15))
1755 break;
1756 goto no_apic;
1757 case X86_VENDOR_INTEL:
1758 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1759 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1760 break;
1761 goto no_apic;
1762 default:
1763 goto no_apic;
1766 if (!cpu_has_apic) {
1768 * Over-ride BIOS and try to enable the local APIC only if
1769 * "lapic" specified.
1771 if (!force_enable_local_apic) {
1772 pr_info("Local APIC disabled by BIOS -- "
1773 "you can enable it with \"lapic\"\n");
1774 return -1;
1776 if (apic_force_enable(APIC_DEFAULT_PHYS_BASE))
1777 return -1;
1778 } else {
1779 if (apic_verify())
1780 return -1;
1783 apic_pm_activate();
1785 return 0;
1787 no_apic:
1788 pr_info("No local APIC present or hardware disabled\n");
1789 return -1;
1791 #endif
1794 * init_apic_mappings - initialize APIC mappings
1796 void __init init_apic_mappings(void)
1798 unsigned int new_apicid;
1800 if (x2apic_mode) {
1801 boot_cpu_physical_apicid = read_apic_id();
1802 return;
1805 /* If no local APIC can be found return early */
1806 if (!smp_found_config && detect_init_APIC()) {
1807 /* lets NOP'ify apic operations */
1808 pr_info("APIC: disable apic facility\n");
1809 apic_disable();
1810 } else {
1811 apic_phys = mp_lapic_addr;
1814 * acpi lapic path already maps that address in
1815 * acpi_register_lapic_address()
1817 if (!acpi_lapic && !smp_found_config)
1818 register_lapic_address(apic_phys);
1822 * Fetch the APIC ID of the BSP in case we have a
1823 * default configuration (or the MP table is broken).
1825 new_apicid = read_apic_id();
1826 if (boot_cpu_physical_apicid != new_apicid) {
1827 boot_cpu_physical_apicid = new_apicid;
1829 * yeah -- we lie about apic_version
1830 * in case if apic was disabled via boot option
1831 * but it's not a problem for SMP compiled kernel
1832 * since smp_sanity_check is prepared for such a case
1833 * and disable smp mode
1835 apic_version[new_apicid] =
1836 GET_APIC_VERSION(apic_read(APIC_LVR));
1840 void __init register_lapic_address(unsigned long address)
1842 mp_lapic_addr = address;
1844 if (!x2apic_mode) {
1845 set_fixmap_nocache(FIX_APIC_BASE, address);
1846 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1847 APIC_BASE, mp_lapic_addr);
1849 if (boot_cpu_physical_apicid == -1U) {
1850 boot_cpu_physical_apicid = read_apic_id();
1851 apic_version[boot_cpu_physical_apicid] =
1852 GET_APIC_VERSION(apic_read(APIC_LVR));
1857 * This initializes the IO-APIC and APIC hardware if this is
1858 * a UP kernel.
1860 int apic_version[MAX_LOCAL_APIC];
1862 int __init APIC_init_uniprocessor(void)
1864 if (disable_apic) {
1865 pr_info("Apic disabled\n");
1866 return -1;
1868 #ifdef CONFIG_X86_64
1869 if (!cpu_has_apic) {
1870 disable_apic = 1;
1871 pr_info("Apic disabled by BIOS\n");
1872 return -1;
1874 #else
1875 if (!smp_found_config && !cpu_has_apic)
1876 return -1;
1879 * Complain if the BIOS pretends there is one.
1881 if (!cpu_has_apic &&
1882 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1883 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1884 boot_cpu_physical_apicid);
1885 return -1;
1887 #endif
1889 default_setup_apic_routing();
1891 verify_local_APIC();
1892 connect_bsp_APIC();
1894 #ifdef CONFIG_X86_64
1895 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
1896 #else
1898 * Hack: In case of kdump, after a crash, kernel might be booting
1899 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1900 * might be zero if read from MP tables. Get it from LAPIC.
1902 # ifdef CONFIG_CRASH_DUMP
1903 boot_cpu_physical_apicid = read_apic_id();
1904 # endif
1905 #endif
1906 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
1907 setup_local_APIC();
1909 #ifdef CONFIG_X86_IO_APIC
1911 * Now enable IO-APICs, actually call clear_IO_APIC
1912 * We need clear_IO_APIC before enabling error vector
1914 if (!skip_ioapic_setup && nr_ioapics)
1915 enable_IO_APIC();
1916 #endif
1918 bsp_end_local_APIC_setup();
1920 #ifdef CONFIG_X86_IO_APIC
1921 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1922 setup_IO_APIC();
1923 else {
1924 nr_ioapics = 0;
1926 #endif
1928 x86_init.timers.setup_percpu_clockev();
1929 return 0;
1933 * Local APIC interrupts
1937 * This interrupt should _never_ happen with our APIC/SMP architecture
1939 static inline void __smp_spurious_interrupt(void)
1941 u32 v;
1944 * Check if this really is a spurious interrupt and ACK it
1945 * if it is a vectored one. Just in case...
1946 * Spurious interrupts should not be ACKed.
1948 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1949 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1950 ack_APIC_irq();
1952 inc_irq_stat(irq_spurious_count);
1954 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1955 pr_info("spurious APIC interrupt on CPU#%d, "
1956 "should never happen.\n", smp_processor_id());
1959 __visible void smp_spurious_interrupt(struct pt_regs *regs)
1961 entering_irq();
1962 __smp_spurious_interrupt();
1963 exiting_irq();
1966 __visible void smp_trace_spurious_interrupt(struct pt_regs *regs)
1968 entering_irq();
1969 trace_spurious_apic_entry(SPURIOUS_APIC_VECTOR);
1970 __smp_spurious_interrupt();
1971 trace_spurious_apic_exit(SPURIOUS_APIC_VECTOR);
1972 exiting_irq();
1976 * This interrupt should never happen with our APIC/SMP architecture
1978 static inline void __smp_error_interrupt(struct pt_regs *regs)
1980 u32 v0, v1;
1981 u32 i = 0;
1982 static const char * const error_interrupt_reason[] = {
1983 "Send CS error", /* APIC Error Bit 0 */
1984 "Receive CS error", /* APIC Error Bit 1 */
1985 "Send accept error", /* APIC Error Bit 2 */
1986 "Receive accept error", /* APIC Error Bit 3 */
1987 "Redirectable IPI", /* APIC Error Bit 4 */
1988 "Send illegal vector", /* APIC Error Bit 5 */
1989 "Received illegal vector", /* APIC Error Bit 6 */
1990 "Illegal register address", /* APIC Error Bit 7 */
1993 /* First tickle the hardware, only then report what went on. -- REW */
1994 v0 = apic_read(APIC_ESR);
1995 apic_write(APIC_ESR, 0);
1996 v1 = apic_read(APIC_ESR);
1997 ack_APIC_irq();
1998 atomic_inc(&irq_err_count);
2000 apic_printk(APIC_DEBUG, KERN_DEBUG "APIC error on CPU%d: %02x(%02x)",
2001 smp_processor_id(), v0 , v1);
2003 v1 = v1 & 0xff;
2004 while (v1) {
2005 if (v1 & 0x1)
2006 apic_printk(APIC_DEBUG, KERN_CONT " : %s", error_interrupt_reason[i]);
2007 i++;
2008 v1 >>= 1;
2011 apic_printk(APIC_DEBUG, KERN_CONT "\n");
2015 __visible void smp_error_interrupt(struct pt_regs *regs)
2017 entering_irq();
2018 __smp_error_interrupt(regs);
2019 exiting_irq();
2022 __visible void smp_trace_error_interrupt(struct pt_regs *regs)
2024 entering_irq();
2025 trace_error_apic_entry(ERROR_APIC_VECTOR);
2026 __smp_error_interrupt(regs);
2027 trace_error_apic_exit(ERROR_APIC_VECTOR);
2028 exiting_irq();
2032 * connect_bsp_APIC - attach the APIC to the interrupt system
2034 void __init connect_bsp_APIC(void)
2036 #ifdef CONFIG_X86_32
2037 if (pic_mode) {
2039 * Do not trust the local APIC being empty at bootup.
2041 clear_local_APIC();
2043 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
2044 * local APIC to INT and NMI lines.
2046 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
2047 "enabling APIC mode.\n");
2048 imcr_pic_to_apic();
2050 #endif
2051 if (apic->enable_apic_mode)
2052 apic->enable_apic_mode();
2056 * disconnect_bsp_APIC - detach the APIC from the interrupt system
2057 * @virt_wire_setup: indicates, whether virtual wire mode is selected
2059 * Virtual wire mode is necessary to deliver legacy interrupts even when the
2060 * APIC is disabled.
2062 void disconnect_bsp_APIC(int virt_wire_setup)
2064 unsigned int value;
2066 #ifdef CONFIG_X86_32
2067 if (pic_mode) {
2069 * Put the board back into PIC mode (has an effect only on
2070 * certain older boards). Note that APIC interrupts, including
2071 * IPIs, won't work beyond this point! The only exception are
2072 * INIT IPIs.
2074 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
2075 "entering PIC mode.\n");
2076 imcr_apic_to_pic();
2077 return;
2079 #endif
2081 /* Go back to Virtual Wire compatibility mode */
2083 /* For the spurious interrupt use vector F, and enable it */
2084 value = apic_read(APIC_SPIV);
2085 value &= ~APIC_VECTOR_MASK;
2086 value |= APIC_SPIV_APIC_ENABLED;
2087 value |= 0xf;
2088 apic_write(APIC_SPIV, value);
2090 if (!virt_wire_setup) {
2092 * For LVT0 make it edge triggered, active high,
2093 * external and enabled
2095 value = apic_read(APIC_LVT0);
2096 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
2097 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
2098 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
2099 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
2100 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
2101 apic_write(APIC_LVT0, value);
2102 } else {
2103 /* Disable LVT0 */
2104 apic_write(APIC_LVT0, APIC_LVT_MASKED);
2108 * For LVT1 make it edge triggered, active high,
2109 * nmi and enabled
2111 value = apic_read(APIC_LVT1);
2112 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
2113 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
2114 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
2115 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
2116 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
2117 apic_write(APIC_LVT1, value);
2120 void generic_processor_info(int apicid, int version)
2122 int cpu, max = nr_cpu_ids;
2123 bool boot_cpu_detected = physid_isset(boot_cpu_physical_apicid,
2124 phys_cpu_present_map);
2127 * If boot cpu has not been detected yet, then only allow upto
2128 * nr_cpu_ids - 1 processors and keep one slot free for boot cpu
2130 if (!boot_cpu_detected && num_processors >= nr_cpu_ids - 1 &&
2131 apicid != boot_cpu_physical_apicid) {
2132 int thiscpu = max + disabled_cpus - 1;
2134 pr_warning(
2135 "ACPI: NR_CPUS/possible_cpus limit of %i almost"
2136 " reached. Keeping one slot for boot cpu."
2137 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
2139 disabled_cpus++;
2140 return;
2143 if (num_processors >= nr_cpu_ids) {
2144 int thiscpu = max + disabled_cpus;
2146 pr_warning(
2147 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
2148 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
2150 disabled_cpus++;
2151 return;
2154 num_processors++;
2155 if (apicid == boot_cpu_physical_apicid) {
2157 * x86_bios_cpu_apicid is required to have processors listed
2158 * in same order as logical cpu numbers. Hence the first
2159 * entry is BSP, and so on.
2160 * boot_cpu_init() already hold bit 0 in cpu_present_mask
2161 * for BSP.
2163 cpu = 0;
2164 } else
2165 cpu = cpumask_next_zero(-1, cpu_present_mask);
2168 * Validate version
2170 if (version == 0x0) {
2171 pr_warning("BIOS bug: APIC version is 0 for CPU %d/0x%x, fixing up to 0x10\n",
2172 cpu, apicid);
2173 version = 0x10;
2175 apic_version[apicid] = version;
2177 if (version != apic_version[boot_cpu_physical_apicid]) {
2178 pr_warning("BIOS bug: APIC version mismatch, boot CPU: %x, CPU %d: version %x\n",
2179 apic_version[boot_cpu_physical_apicid], cpu, version);
2182 physid_set(apicid, phys_cpu_present_map);
2183 if (apicid > max_physical_apicid)
2184 max_physical_apicid = apicid;
2186 #if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
2187 early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
2188 early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
2189 #endif
2190 #ifdef CONFIG_X86_32
2191 early_per_cpu(x86_cpu_to_logical_apicid, cpu) =
2192 apic->x86_32_early_logical_apicid(cpu);
2193 #endif
2194 set_cpu_possible(cpu, true);
2195 set_cpu_present(cpu, true);
2198 int hard_smp_processor_id(void)
2200 return read_apic_id();
2203 void default_init_apic_ldr(void)
2205 unsigned long val;
2207 apic_write(APIC_DFR, APIC_DFR_VALUE);
2208 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
2209 val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
2210 apic_write(APIC_LDR, val);
2213 int default_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
2214 const struct cpumask *andmask,
2215 unsigned int *apicid)
2217 unsigned int cpu;
2219 for_each_cpu_and(cpu, cpumask, andmask) {
2220 if (cpumask_test_cpu(cpu, cpu_online_mask))
2221 break;
2224 if (likely(cpu < nr_cpu_ids)) {
2225 *apicid = per_cpu(x86_cpu_to_apicid, cpu);
2226 return 0;
2229 return -EINVAL;
2233 * Override the generic EOI implementation with an optimized version.
2234 * Only called during early boot when only one CPU is active and with
2235 * interrupts disabled, so we know this does not race with actual APIC driver
2236 * use.
2238 void __init apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v))
2240 struct apic **drv;
2242 for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) {
2243 /* Should happen once for each apic */
2244 WARN_ON((*drv)->eoi_write == eoi_write);
2245 (*drv)->eoi_write = eoi_write;
2250 * Power management
2252 #ifdef CONFIG_PM
2254 static struct {
2256 * 'active' is true if the local APIC was enabled by us and
2257 * not the BIOS; this signifies that we are also responsible
2258 * for disabling it before entering apm/acpi suspend
2260 int active;
2261 /* r/w apic fields */
2262 unsigned int apic_id;
2263 unsigned int apic_taskpri;
2264 unsigned int apic_ldr;
2265 unsigned int apic_dfr;
2266 unsigned int apic_spiv;
2267 unsigned int apic_lvtt;
2268 unsigned int apic_lvtpc;
2269 unsigned int apic_lvt0;
2270 unsigned int apic_lvt1;
2271 unsigned int apic_lvterr;
2272 unsigned int apic_tmict;
2273 unsigned int apic_tdcr;
2274 unsigned int apic_thmr;
2275 } apic_pm_state;
2277 static int lapic_suspend(void)
2279 unsigned long flags;
2280 int maxlvt;
2282 if (!apic_pm_state.active)
2283 return 0;
2285 maxlvt = lapic_get_maxlvt();
2287 apic_pm_state.apic_id = apic_read(APIC_ID);
2288 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
2289 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
2290 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
2291 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
2292 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
2293 if (maxlvt >= 4)
2294 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
2295 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
2296 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
2297 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
2298 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
2299 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
2300 #ifdef CONFIG_X86_THERMAL_VECTOR
2301 if (maxlvt >= 5)
2302 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
2303 #endif
2305 local_irq_save(flags);
2306 disable_local_APIC();
2308 irq_remapping_disable();
2310 local_irq_restore(flags);
2311 return 0;
2314 static void lapic_resume(void)
2316 unsigned int l, h;
2317 unsigned long flags;
2318 int maxlvt;
2320 if (!apic_pm_state.active)
2321 return;
2323 local_irq_save(flags);
2326 * IO-APIC and PIC have their own resume routines.
2327 * We just mask them here to make sure the interrupt
2328 * subsystem is completely quiet while we enable x2apic
2329 * and interrupt-remapping.
2331 mask_ioapic_entries();
2332 legacy_pic->mask_all();
2334 if (x2apic_mode)
2335 enable_x2apic();
2336 else {
2338 * Make sure the APICBASE points to the right address
2340 * FIXME! This will be wrong if we ever support suspend on
2341 * SMP! We'll need to do this as part of the CPU restore!
2343 if (boot_cpu_data.x86 >= 6) {
2344 rdmsr(MSR_IA32_APICBASE, l, h);
2345 l &= ~MSR_IA32_APICBASE_BASE;
2346 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2347 wrmsr(MSR_IA32_APICBASE, l, h);
2351 maxlvt = lapic_get_maxlvt();
2352 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2353 apic_write(APIC_ID, apic_pm_state.apic_id);
2354 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2355 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2356 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2357 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2358 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2359 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
2360 #if defined(CONFIG_X86_MCE_INTEL)
2361 if (maxlvt >= 5)
2362 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2363 #endif
2364 if (maxlvt >= 4)
2365 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
2366 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2367 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2368 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2369 apic_write(APIC_ESR, 0);
2370 apic_read(APIC_ESR);
2371 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2372 apic_write(APIC_ESR, 0);
2373 apic_read(APIC_ESR);
2375 irq_remapping_reenable(x2apic_mode);
2377 local_irq_restore(flags);
2381 * This device has no shutdown method - fully functioning local APICs
2382 * are needed on every CPU up until machine_halt/restart/poweroff.
2385 static struct syscore_ops lapic_syscore_ops = {
2386 .resume = lapic_resume,
2387 .suspend = lapic_suspend,
2390 static void apic_pm_activate(void)
2392 apic_pm_state.active = 1;
2395 static int __init init_lapic_sysfs(void)
2397 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
2398 if (cpu_has_apic)
2399 register_syscore_ops(&lapic_syscore_ops);
2401 return 0;
2404 /* local apic needs to resume before other devices access its registers. */
2405 core_initcall(init_lapic_sysfs);
2407 #else /* CONFIG_PM */
2409 static void apic_pm_activate(void) { }
2411 #endif /* CONFIG_PM */
2413 #ifdef CONFIG_X86_64
2415 static int apic_cluster_num(void)
2417 int i, clusters, zeros;
2418 unsigned id;
2419 u16 *bios_cpu_apicid;
2420 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
2422 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
2423 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
2425 for (i = 0; i < nr_cpu_ids; i++) {
2426 /* are we being called early in kernel startup? */
2427 if (bios_cpu_apicid) {
2428 id = bios_cpu_apicid[i];
2429 } else if (i < nr_cpu_ids) {
2430 if (cpu_present(i))
2431 id = per_cpu(x86_bios_cpu_apicid, i);
2432 else
2433 continue;
2434 } else
2435 break;
2437 if (id != BAD_APICID)
2438 __set_bit(APIC_CLUSTERID(id), clustermap);
2441 /* Problem: Partially populated chassis may not have CPUs in some of
2442 * the APIC clusters they have been allocated. Only present CPUs have
2443 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2444 * Since clusters are allocated sequentially, count zeros only if
2445 * they are bounded by ones.
2447 clusters = 0;
2448 zeros = 0;
2449 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
2450 if (test_bit(i, clustermap)) {
2451 clusters += 1 + zeros;
2452 zeros = 0;
2453 } else
2454 ++zeros;
2457 return clusters;
2460 static int multi_checked;
2461 static int multi;
2463 static int set_multi(const struct dmi_system_id *d)
2465 if (multi)
2466 return 0;
2467 pr_info("APIC: %s detected, Multi Chassis\n", d->ident);
2468 multi = 1;
2469 return 0;
2472 static const struct dmi_system_id multi_dmi_table[] = {
2474 .callback = set_multi,
2475 .ident = "IBM System Summit2",
2476 .matches = {
2477 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
2478 DMI_MATCH(DMI_PRODUCT_NAME, "Summit2"),
2484 static void dmi_check_multi(void)
2486 if (multi_checked)
2487 return;
2489 dmi_check_system(multi_dmi_table);
2490 multi_checked = 1;
2494 * apic_is_clustered_box() -- Check if we can expect good TSC
2496 * Thus far, the major user of this is IBM's Summit2 series:
2497 * Clustered boxes may have unsynced TSC problems if they are
2498 * multi-chassis.
2499 * Use DMI to check them
2501 int apic_is_clustered_box(void)
2503 dmi_check_multi();
2504 if (multi)
2505 return 1;
2507 if (!is_vsmp_box())
2508 return 0;
2511 * ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2512 * not guaranteed to be synced between boards
2514 if (apic_cluster_num() > 1)
2515 return 1;
2517 return 0;
2519 #endif
2522 * APIC command line parameters
2524 static int __init setup_disableapic(char *arg)
2526 disable_apic = 1;
2527 setup_clear_cpu_cap(X86_FEATURE_APIC);
2528 return 0;
2530 early_param("disableapic", setup_disableapic);
2532 /* same as disableapic, for compatibility */
2533 static int __init setup_nolapic(char *arg)
2535 return setup_disableapic(arg);
2537 early_param("nolapic", setup_nolapic);
2539 static int __init parse_lapic_timer_c2_ok(char *arg)
2541 local_apic_timer_c2_ok = 1;
2542 return 0;
2544 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2546 static int __init parse_disable_apic_timer(char *arg)
2548 disable_apic_timer = 1;
2549 return 0;
2551 early_param("noapictimer", parse_disable_apic_timer);
2553 static int __init parse_nolapic_timer(char *arg)
2555 disable_apic_timer = 1;
2556 return 0;
2558 early_param("nolapic_timer", parse_nolapic_timer);
2560 static int __init apic_set_verbosity(char *arg)
2562 if (!arg) {
2563 #ifdef CONFIG_X86_64
2564 skip_ioapic_setup = 0;
2565 return 0;
2566 #endif
2567 return -EINVAL;
2570 if (strcmp("debug", arg) == 0)
2571 apic_verbosity = APIC_DEBUG;
2572 else if (strcmp("verbose", arg) == 0)
2573 apic_verbosity = APIC_VERBOSE;
2574 else {
2575 pr_warning("APIC Verbosity level %s not recognised"
2576 " use apic=verbose or apic=debug\n", arg);
2577 return -EINVAL;
2580 return 0;
2582 early_param("apic", apic_set_verbosity);
2584 static int __init lapic_insert_resource(void)
2586 if (!apic_phys)
2587 return -1;
2589 /* Put local APIC into the resource map. */
2590 lapic_resource.start = apic_phys;
2591 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2592 insert_resource(&iomem_resource, &lapic_resource);
2594 return 0;
2598 * need call insert after e820_reserve_resources()
2599 * that is using request_resource
2601 late_initcall(lapic_insert_resource);