x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / arch / x86 / kernel / apic / apic.c
blob2e64178f284da29666a9f4108c3f5614377575b1
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/export.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>
37 #include <linux/irq.h>
39 #include <asm/trace/irq_vectors.h>
40 #include <asm/irq_remapping.h>
41 #include <asm/perf_event.h>
42 #include <asm/x86_init.h>
43 #include <asm/pgalloc.h>
44 #include <linux/atomic.h>
45 #include <asm/mpspec.h>
46 #include <asm/i8259.h>
47 #include <asm/proto.h>
48 #include <asm/apic.h>
49 #include <asm/io_apic.h>
50 #include <asm/desc.h>
51 #include <asm/hpet.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>
58 #include <asm/cpu_device_id.h>
59 #include <asm/intel-family.h>
60 #include <asm/irq_regs.h>
62 unsigned int num_processors;
64 unsigned disabled_cpus;
66 /* Processor that is doing the boot up */
67 unsigned int boot_cpu_physical_apicid = -1U;
68 EXPORT_SYMBOL_GPL(boot_cpu_physical_apicid);
70 u8 boot_cpu_apic_version;
73 * The highest APIC ID seen during enumeration.
75 static unsigned int max_physical_apicid;
78 * Bitmask of physically existing CPUs:
80 physid_mask_t phys_cpu_present_map;
83 * Processor to be disabled specified by kernel parameter
84 * disable_cpu_apicid=<int>, mostly used for the kdump 2nd kernel to
85 * avoid undefined behaviour caused by sending INIT from AP to BSP.
87 static unsigned int disabled_cpu_apicid __read_mostly = BAD_APICID;
90 * This variable controls which CPUs receive external NMIs. By default,
91 * external NMIs are delivered only to the BSP.
93 static int apic_extnmi = APIC_EXTNMI_BSP;
96 * Map cpu index to physical APIC ID
98 DEFINE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_cpu_to_apicid, BAD_APICID);
99 DEFINE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_bios_cpu_apicid, BAD_APICID);
100 DEFINE_EARLY_PER_CPU_READ_MOSTLY(u32, x86_cpu_to_acpiid, U32_MAX);
101 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
102 EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
103 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_acpiid);
105 #ifdef CONFIG_X86_32
108 * On x86_32, the mapping between cpu and logical apicid may vary
109 * depending on apic in use. The following early percpu variable is
110 * used for the mapping. This is where the behaviors of x86_64 and 32
111 * actually diverge. Let's keep it ugly for now.
113 DEFINE_EARLY_PER_CPU_READ_MOSTLY(int, x86_cpu_to_logical_apicid, BAD_APICID);
115 /* Local APIC was disabled by the BIOS and enabled by the kernel */
116 static int enabled_via_apicbase;
119 * Handle interrupt mode configuration register (IMCR).
120 * This register controls whether the interrupt signals
121 * that reach the BSP come from the master PIC or from the
122 * local APIC. Before entering Symmetric I/O Mode, either
123 * the BIOS or the operating system must switch out of
124 * PIC Mode by changing the IMCR.
126 static inline void imcr_pic_to_apic(void)
128 /* select IMCR register */
129 outb(0x70, 0x22);
130 /* NMI and 8259 INTR go through APIC */
131 outb(0x01, 0x23);
134 static inline void imcr_apic_to_pic(void)
136 /* select IMCR register */
137 outb(0x70, 0x22);
138 /* NMI and 8259 INTR go directly to BSP */
139 outb(0x00, 0x23);
141 #endif
144 * Knob to control our willingness to enable the local APIC.
146 * +1=force-enable
148 static int force_enable_local_apic __initdata;
151 * APIC command line parameters
153 static int __init parse_lapic(char *arg)
155 if (IS_ENABLED(CONFIG_X86_32) && !arg)
156 force_enable_local_apic = 1;
157 else if (arg && !strncmp(arg, "notscdeadline", 13))
158 setup_clear_cpu_cap(X86_FEATURE_TSC_DEADLINE_TIMER);
159 return 0;
161 early_param("lapic", parse_lapic);
163 #ifdef CONFIG_X86_64
164 static int apic_calibrate_pmtmr __initdata;
165 static __init int setup_apicpmtimer(char *s)
167 apic_calibrate_pmtmr = 1;
168 notsc_setup(NULL);
169 return 0;
171 __setup("apicpmtimer", setup_apicpmtimer);
172 #endif
174 unsigned long mp_lapic_addr;
175 int disable_apic;
176 /* Disable local APIC timer from the kernel commandline or via dmi quirk */
177 static int disable_apic_timer __initdata;
178 /* Local APIC timer works in C2 */
179 int local_apic_timer_c2_ok;
180 EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
183 * Debug level, exported for io_apic.c
185 unsigned int apic_verbosity;
187 int pic_mode;
189 /* Have we found an MP table */
190 int smp_found_config;
192 static struct resource lapic_resource = {
193 .name = "Local APIC",
194 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
197 unsigned int lapic_timer_frequency = 0;
199 static void apic_pm_activate(void);
201 static unsigned long apic_phys;
204 * Get the LAPIC version
206 static inline int lapic_get_version(void)
208 return GET_APIC_VERSION(apic_read(APIC_LVR));
212 * Check, if the APIC is integrated or a separate chip
214 static inline int lapic_is_integrated(void)
216 #ifdef CONFIG_X86_64
217 return 1;
218 #else
219 return APIC_INTEGRATED(lapic_get_version());
220 #endif
224 * Check, whether this is a modern or a first generation APIC
226 static int modern_apic(void)
228 /* AMD systems use old APIC versions, so check the CPU */
229 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
230 boot_cpu_data.x86 >= 0xf)
231 return 1;
232 return lapic_get_version() >= 0x14;
236 * right after this call apic become NOOP driven
237 * so apic->write/read doesn't do anything
239 static void __init apic_disable(void)
241 pr_info("APIC: switched to apic NOOP\n");
242 apic = &apic_noop;
245 void native_apic_wait_icr_idle(void)
247 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
248 cpu_relax();
251 u32 native_safe_apic_wait_icr_idle(void)
253 u32 send_status;
254 int timeout;
256 timeout = 0;
257 do {
258 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
259 if (!send_status)
260 break;
261 inc_irq_stat(icr_read_retry_count);
262 udelay(100);
263 } while (timeout++ < 1000);
265 return send_status;
268 void native_apic_icr_write(u32 low, u32 id)
270 unsigned long flags;
272 local_irq_save(flags);
273 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
274 apic_write(APIC_ICR, low);
275 local_irq_restore(flags);
278 u64 native_apic_icr_read(void)
280 u32 icr1, icr2;
282 icr2 = apic_read(APIC_ICR2);
283 icr1 = apic_read(APIC_ICR);
285 return icr1 | ((u64)icr2 << 32);
288 #ifdef CONFIG_X86_32
290 * get_physical_broadcast - Get number of physical broadcast IDs
292 int get_physical_broadcast(void)
294 return modern_apic() ? 0xff : 0xf;
296 #endif
299 * lapic_get_maxlvt - get the maximum number of local vector table entries
301 int lapic_get_maxlvt(void)
303 unsigned int v;
305 v = apic_read(APIC_LVR);
307 * - we always have APIC integrated on 64bit mode
308 * - 82489DXs do not report # of LVT entries
310 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
314 * Local APIC timer
317 /* Clock divisor */
318 #define APIC_DIVISOR 16
319 #define TSC_DIVISOR 8
322 * This function sets up the local APIC timer, with a timeout of
323 * 'clocks' APIC bus clock. During calibration we actually call
324 * this function twice on the boot CPU, once with a bogus timeout
325 * value, second time for real. The other (noncalibrating) CPUs
326 * call this function only once, with the real, calibrated value.
328 * We do reads before writes even if unnecessary, to get around the
329 * P5 APIC double write bug.
331 static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
333 unsigned int lvtt_value, tmp_value;
335 lvtt_value = LOCAL_TIMER_VECTOR;
336 if (!oneshot)
337 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
338 else if (boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER))
339 lvtt_value |= APIC_LVT_TIMER_TSCDEADLINE;
341 if (!lapic_is_integrated())
342 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
344 if (!irqen)
345 lvtt_value |= APIC_LVT_MASKED;
347 apic_write(APIC_LVTT, lvtt_value);
349 if (lvtt_value & APIC_LVT_TIMER_TSCDEADLINE) {
351 * See Intel SDM: TSC-Deadline Mode chapter. In xAPIC mode,
352 * writing to the APIC LVTT and TSC_DEADLINE MSR isn't serialized.
353 * According to Intel, MFENCE can do the serialization here.
355 asm volatile("mfence" : : : "memory");
357 printk_once(KERN_DEBUG "TSC deadline timer enabled\n");
358 return;
362 * Divide PICLK by 16
364 tmp_value = apic_read(APIC_TDCR);
365 apic_write(APIC_TDCR,
366 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
367 APIC_TDR_DIV_16);
369 if (!oneshot)
370 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
374 * Setup extended LVT, AMD specific
376 * Software should use the LVT offsets the BIOS provides. The offsets
377 * are determined by the subsystems using it like those for MCE
378 * threshold or IBS. On K8 only offset 0 (APIC500) and MCE interrupts
379 * are supported. Beginning with family 10h at least 4 offsets are
380 * available.
382 * Since the offsets must be consistent for all cores, we keep track
383 * of the LVT offsets in software and reserve the offset for the same
384 * vector also to be used on other cores. An offset is freed by
385 * setting the entry to APIC_EILVT_MASKED.
387 * If the BIOS is right, there should be no conflicts. Otherwise a
388 * "[Firmware Bug]: ..." error message is generated. However, if
389 * software does not properly determines the offsets, it is not
390 * necessarily a BIOS bug.
393 static atomic_t eilvt_offsets[APIC_EILVT_NR_MAX];
395 static inline int eilvt_entry_is_changeable(unsigned int old, unsigned int new)
397 return (old & APIC_EILVT_MASKED)
398 || (new == APIC_EILVT_MASKED)
399 || ((new & ~APIC_EILVT_MASKED) == old);
402 static unsigned int reserve_eilvt_offset(int offset, unsigned int new)
404 unsigned int rsvd, vector;
406 if (offset >= APIC_EILVT_NR_MAX)
407 return ~0;
409 rsvd = atomic_read(&eilvt_offsets[offset]);
410 do {
411 vector = rsvd & ~APIC_EILVT_MASKED; /* 0: unassigned */
412 if (vector && !eilvt_entry_is_changeable(vector, new))
413 /* may not change if vectors are different */
414 return rsvd;
415 rsvd = atomic_cmpxchg(&eilvt_offsets[offset], rsvd, new);
416 } while (rsvd != new);
418 rsvd &= ~APIC_EILVT_MASKED;
419 if (rsvd && rsvd != vector)
420 pr_info("LVT offset %d assigned for vector 0x%02x\n",
421 offset, rsvd);
423 return new;
427 * If mask=1, the LVT entry does not generate interrupts while mask=0
428 * enables the vector. See also the BKDGs. Must be called with
429 * preemption disabled.
432 int setup_APIC_eilvt(u8 offset, u8 vector, u8 msg_type, u8 mask)
434 unsigned long reg = APIC_EILVTn(offset);
435 unsigned int new, old, reserved;
437 new = (mask << 16) | (msg_type << 8) | vector;
438 old = apic_read(reg);
439 reserved = reserve_eilvt_offset(offset, new);
441 if (reserved != new) {
442 pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
443 "vector 0x%x, but the register is already in use for "
444 "vector 0x%x on another cpu\n",
445 smp_processor_id(), reg, offset, new, reserved);
446 return -EINVAL;
449 if (!eilvt_entry_is_changeable(old, new)) {
450 pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
451 "vector 0x%x, but the register is already in use for "
452 "vector 0x%x on this cpu\n",
453 smp_processor_id(), reg, offset, new, old);
454 return -EBUSY;
457 apic_write(reg, new);
459 return 0;
461 EXPORT_SYMBOL_GPL(setup_APIC_eilvt);
464 * Program the next event, relative to now
466 static int lapic_next_event(unsigned long delta,
467 struct clock_event_device *evt)
469 apic_write(APIC_TMICT, delta);
470 return 0;
473 static int lapic_next_deadline(unsigned long delta,
474 struct clock_event_device *evt)
476 u64 tsc;
478 tsc = rdtsc();
479 wrmsrl(MSR_IA32_TSC_DEADLINE, tsc + (((u64) delta) * TSC_DIVISOR));
480 return 0;
483 static int lapic_timer_shutdown(struct clock_event_device *evt)
485 unsigned int v;
487 /* Lapic used as dummy for broadcast ? */
488 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
489 return 0;
491 v = apic_read(APIC_LVTT);
492 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
493 apic_write(APIC_LVTT, v);
494 apic_write(APIC_TMICT, 0);
495 return 0;
498 static inline int
499 lapic_timer_set_periodic_oneshot(struct clock_event_device *evt, bool oneshot)
501 /* Lapic used as dummy for broadcast ? */
502 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
503 return 0;
505 __setup_APIC_LVTT(lapic_timer_frequency, oneshot, 1);
506 return 0;
509 static int lapic_timer_set_periodic(struct clock_event_device *evt)
511 return lapic_timer_set_periodic_oneshot(evt, false);
514 static int lapic_timer_set_oneshot(struct clock_event_device *evt)
516 return lapic_timer_set_periodic_oneshot(evt, true);
520 * Local APIC timer broadcast function
522 static void lapic_timer_broadcast(const struct cpumask *mask)
524 #ifdef CONFIG_SMP
525 apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
526 #endif
531 * The local apic timer can be used for any function which is CPU local.
533 static struct clock_event_device lapic_clockevent = {
534 .name = "lapic",
535 .features = CLOCK_EVT_FEAT_PERIODIC |
536 CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_C3STOP
537 | CLOCK_EVT_FEAT_DUMMY,
538 .shift = 32,
539 .set_state_shutdown = lapic_timer_shutdown,
540 .set_state_periodic = lapic_timer_set_periodic,
541 .set_state_oneshot = lapic_timer_set_oneshot,
542 .set_state_oneshot_stopped = lapic_timer_shutdown,
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);
550 #define DEADLINE_MODEL_MATCH_FUNC(model, func) \
551 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&func }
553 #define DEADLINE_MODEL_MATCH_REV(model, rev) \
554 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)rev }
556 static u32 hsx_deadline_rev(void)
558 switch (boot_cpu_data.x86_stepping) {
559 case 0x02: return 0x3a; /* EP */
560 case 0x04: return 0x0f; /* EX */
563 return ~0U;
566 static u32 bdx_deadline_rev(void)
568 switch (boot_cpu_data.x86_stepping) {
569 case 0x02: return 0x00000011;
570 case 0x03: return 0x0700000e;
571 case 0x04: return 0x0f00000c;
572 case 0x05: return 0x0e000003;
575 return ~0U;
578 static u32 skx_deadline_rev(void)
580 switch (boot_cpu_data.x86_stepping) {
581 case 0x03: return 0x01000136;
582 case 0x04: return 0x02000014;
585 if (boot_cpu_data.x86_stepping > 4)
586 return 0;
588 return ~0U;
591 static const struct x86_cpu_id deadline_match[] = {
592 DEADLINE_MODEL_MATCH_FUNC( INTEL_FAM6_HASWELL_X, hsx_deadline_rev),
593 DEADLINE_MODEL_MATCH_REV ( INTEL_FAM6_BROADWELL_X, 0x0b000020),
594 DEADLINE_MODEL_MATCH_FUNC( INTEL_FAM6_BROADWELL_XEON_D, bdx_deadline_rev),
595 DEADLINE_MODEL_MATCH_FUNC( INTEL_FAM6_SKYLAKE_X, skx_deadline_rev),
597 DEADLINE_MODEL_MATCH_REV ( INTEL_FAM6_HASWELL_CORE, 0x22),
598 DEADLINE_MODEL_MATCH_REV ( INTEL_FAM6_HASWELL_ULT, 0x20),
599 DEADLINE_MODEL_MATCH_REV ( INTEL_FAM6_HASWELL_GT3E, 0x17),
601 DEADLINE_MODEL_MATCH_REV ( INTEL_FAM6_BROADWELL_CORE, 0x25),
602 DEADLINE_MODEL_MATCH_REV ( INTEL_FAM6_BROADWELL_GT3E, 0x17),
604 DEADLINE_MODEL_MATCH_REV ( INTEL_FAM6_SKYLAKE_MOBILE, 0xb2),
605 DEADLINE_MODEL_MATCH_REV ( INTEL_FAM6_SKYLAKE_DESKTOP, 0xb2),
607 DEADLINE_MODEL_MATCH_REV ( INTEL_FAM6_KABYLAKE_MOBILE, 0x52),
608 DEADLINE_MODEL_MATCH_REV ( INTEL_FAM6_KABYLAKE_DESKTOP, 0x52),
613 static void apic_check_deadline_errata(void)
615 const struct x86_cpu_id *m;
616 u32 rev;
618 if (!boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER) ||
619 boot_cpu_has(X86_FEATURE_HYPERVISOR))
620 return;
622 m = x86_match_cpu(deadline_match);
623 if (!m)
624 return;
627 * Function pointers will have the MSB set due to address layout,
628 * immediate revisions will not.
630 if ((long)m->driver_data < 0)
631 rev = ((u32 (*)(void))(m->driver_data))();
632 else
633 rev = (u32)m->driver_data;
635 if (boot_cpu_data.microcode >= rev)
636 return;
638 setup_clear_cpu_cap(X86_FEATURE_TSC_DEADLINE_TIMER);
639 pr_err(FW_BUG "TSC_DEADLINE disabled due to Errata; "
640 "please update microcode to version: 0x%x (or later)\n", rev);
644 * Setup the local APIC timer for this CPU. Copy the initialized values
645 * of the boot CPU and register the clock event in the framework.
647 static void setup_APIC_timer(void)
649 struct clock_event_device *levt = this_cpu_ptr(&lapic_events);
651 if (this_cpu_has(X86_FEATURE_ARAT)) {
652 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_C3STOP;
653 /* Make LAPIC timer preferrable over percpu HPET */
654 lapic_clockevent.rating = 150;
657 memcpy(levt, &lapic_clockevent, sizeof(*levt));
658 levt->cpumask = cpumask_of(smp_processor_id());
660 if (this_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER)) {
661 levt->name = "lapic-deadline";
662 levt->features &= ~(CLOCK_EVT_FEAT_PERIODIC |
663 CLOCK_EVT_FEAT_DUMMY);
664 levt->set_next_event = lapic_next_deadline;
665 clockevents_config_and_register(levt,
666 tsc_khz * (1000 / TSC_DIVISOR),
667 0xF, ~0UL);
668 } else
669 clockevents_register_device(levt);
673 * Install the updated TSC frequency from recalibration at the TSC
674 * deadline clockevent devices.
676 static void __lapic_update_tsc_freq(void *info)
678 struct clock_event_device *levt = this_cpu_ptr(&lapic_events);
680 if (!this_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER))
681 return;
683 clockevents_update_freq(levt, tsc_khz * (1000 / TSC_DIVISOR));
686 void lapic_update_tsc_freq(void)
689 * The clockevent device's ->mult and ->shift can both be
690 * changed. In order to avoid races, schedule the frequency
691 * update code on each CPU.
693 on_each_cpu(__lapic_update_tsc_freq, NULL, 0);
697 * In this functions we calibrate APIC bus clocks to the external timer.
699 * We want to do the calibration only once since we want to have local timer
700 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
701 * frequency.
703 * This was previously done by reading the PIT/HPET and waiting for a wrap
704 * around to find out, that a tick has elapsed. I have a box, where the PIT
705 * readout is broken, so it never gets out of the wait loop again. This was
706 * also reported by others.
708 * Monitoring the jiffies value is inaccurate and the clockevents
709 * infrastructure allows us to do a simple substitution of the interrupt
710 * handler.
712 * The calibration routine also uses the pm_timer when possible, as the PIT
713 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
714 * back to normal later in the boot process).
717 #define LAPIC_CAL_LOOPS (HZ/10)
719 static __initdata int lapic_cal_loops = -1;
720 static __initdata long lapic_cal_t1, lapic_cal_t2;
721 static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
722 static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
723 static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
726 * Temporary interrupt handler.
728 static void __init lapic_cal_handler(struct clock_event_device *dev)
730 unsigned long long tsc = 0;
731 long tapic = apic_read(APIC_TMCCT);
732 unsigned long pm = acpi_pm_read_early();
734 if (boot_cpu_has(X86_FEATURE_TSC))
735 tsc = rdtsc();
737 switch (lapic_cal_loops++) {
738 case 0:
739 lapic_cal_t1 = tapic;
740 lapic_cal_tsc1 = tsc;
741 lapic_cal_pm1 = pm;
742 lapic_cal_j1 = jiffies;
743 break;
745 case LAPIC_CAL_LOOPS:
746 lapic_cal_t2 = tapic;
747 lapic_cal_tsc2 = tsc;
748 if (pm < lapic_cal_pm1)
749 pm += ACPI_PM_OVRRUN;
750 lapic_cal_pm2 = pm;
751 lapic_cal_j2 = jiffies;
752 break;
756 static int __init
757 calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc)
759 const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
760 const long pm_thresh = pm_100ms / 100;
761 unsigned long mult;
762 u64 res;
764 #ifndef CONFIG_X86_PM_TIMER
765 return -1;
766 #endif
768 apic_printk(APIC_VERBOSE, "... PM-Timer delta = %ld\n", deltapm);
770 /* Check, if the PM timer is available */
771 if (!deltapm)
772 return -1;
774 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
776 if (deltapm > (pm_100ms - pm_thresh) &&
777 deltapm < (pm_100ms + pm_thresh)) {
778 apic_printk(APIC_VERBOSE, "... PM-Timer result ok\n");
779 return 0;
782 res = (((u64)deltapm) * mult) >> 22;
783 do_div(res, 1000000);
784 pr_warning("APIC calibration not consistent "
785 "with PM-Timer: %ldms instead of 100ms\n",(long)res);
787 /* Correct the lapic counter value */
788 res = (((u64)(*delta)) * pm_100ms);
789 do_div(res, deltapm);
790 pr_info("APIC delta adjusted to PM-Timer: "
791 "%lu (%ld)\n", (unsigned long)res, *delta);
792 *delta = (long)res;
794 /* Correct the tsc counter value */
795 if (boot_cpu_has(X86_FEATURE_TSC)) {
796 res = (((u64)(*deltatsc)) * pm_100ms);
797 do_div(res, deltapm);
798 apic_printk(APIC_VERBOSE, "TSC delta adjusted to "
799 "PM-Timer: %lu (%ld)\n",
800 (unsigned long)res, *deltatsc);
801 *deltatsc = (long)res;
804 return 0;
807 static int __init calibrate_APIC_clock(void)
809 struct clock_event_device *levt = this_cpu_ptr(&lapic_events);
810 void (*real_handler)(struct clock_event_device *dev);
811 unsigned long deltaj;
812 long delta, deltatsc;
813 int pm_referenced = 0;
816 * check if lapic timer has already been calibrated by platform
817 * specific routine, such as tsc calibration code. if so, we just fill
818 * in the clockevent structure and return.
821 if (boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER)) {
822 return 0;
823 } else if (lapic_timer_frequency) {
824 apic_printk(APIC_VERBOSE, "lapic timer already calibrated %d\n",
825 lapic_timer_frequency);
826 lapic_clockevent.mult = div_sc(lapic_timer_frequency/APIC_DIVISOR,
827 TICK_NSEC, lapic_clockevent.shift);
828 lapic_clockevent.max_delta_ns =
829 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
830 lapic_clockevent.max_delta_ticks = 0x7FFFFF;
831 lapic_clockevent.min_delta_ns =
832 clockevent_delta2ns(0xF, &lapic_clockevent);
833 lapic_clockevent.min_delta_ticks = 0xF;
834 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
835 return 0;
838 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
839 "calibrating APIC timer ...\n");
841 local_irq_disable();
843 /* Replace the global interrupt handler */
844 real_handler = global_clock_event->event_handler;
845 global_clock_event->event_handler = lapic_cal_handler;
848 * Setup the APIC counter to maximum. There is no way the lapic
849 * can underflow in the 100ms detection time frame
851 __setup_APIC_LVTT(0xffffffff, 0, 0);
853 /* Let the interrupts run */
854 local_irq_enable();
856 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
857 cpu_relax();
859 local_irq_disable();
861 /* Restore the real event handler */
862 global_clock_event->event_handler = real_handler;
864 /* Build delta t1-t2 as apic timer counts down */
865 delta = lapic_cal_t1 - lapic_cal_t2;
866 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
868 deltatsc = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
870 /* we trust the PM based calibration if possible */
871 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
872 &delta, &deltatsc);
874 /* Calculate the scaled math multiplication factor */
875 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
876 lapic_clockevent.shift);
877 lapic_clockevent.max_delta_ns =
878 clockevent_delta2ns(0x7FFFFFFF, &lapic_clockevent);
879 lapic_clockevent.max_delta_ticks = 0x7FFFFFFF;
880 lapic_clockevent.min_delta_ns =
881 clockevent_delta2ns(0xF, &lapic_clockevent);
882 lapic_clockevent.min_delta_ticks = 0xF;
884 lapic_timer_frequency = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
886 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
887 apic_printk(APIC_VERBOSE, "..... mult: %u\n", lapic_clockevent.mult);
888 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
889 lapic_timer_frequency);
891 if (boot_cpu_has(X86_FEATURE_TSC)) {
892 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
893 "%ld.%04ld MHz.\n",
894 (deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
895 (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
898 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
899 "%u.%04u MHz.\n",
900 lapic_timer_frequency / (1000000 / HZ),
901 lapic_timer_frequency % (1000000 / HZ));
904 * Do a sanity check on the APIC calibration result
906 if (lapic_timer_frequency < (1000000 / HZ)) {
907 local_irq_enable();
908 pr_warning("APIC frequency too slow, disabling apic timer\n");
909 return -1;
912 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
915 * PM timer calibration failed or not turned on
916 * so lets try APIC timer based calibration
918 if (!pm_referenced) {
919 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
922 * Setup the apic timer manually
924 levt->event_handler = lapic_cal_handler;
925 lapic_timer_set_periodic(levt);
926 lapic_cal_loops = -1;
928 /* Let the interrupts run */
929 local_irq_enable();
931 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
932 cpu_relax();
934 /* Stop the lapic timer */
935 local_irq_disable();
936 lapic_timer_shutdown(levt);
938 /* Jiffies delta */
939 deltaj = lapic_cal_j2 - lapic_cal_j1;
940 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
942 /* Check, if the jiffies result is consistent */
943 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
944 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
945 else
946 levt->features |= CLOCK_EVT_FEAT_DUMMY;
948 local_irq_enable();
950 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
951 pr_warning("APIC timer disabled due to verification failure\n");
952 return -1;
955 return 0;
959 * Setup the boot APIC
961 * Calibrate and verify the result.
963 void __init setup_boot_APIC_clock(void)
966 * The local apic timer can be disabled via the kernel
967 * commandline or from the CPU detection code. Register the lapic
968 * timer as a dummy clock event source on SMP systems, so the
969 * broadcast mechanism is used. On UP systems simply ignore it.
971 if (disable_apic_timer) {
972 pr_info("Disabling APIC timer\n");
973 /* No broadcast on UP ! */
974 if (num_possible_cpus() > 1) {
975 lapic_clockevent.mult = 1;
976 setup_APIC_timer();
978 return;
981 if (calibrate_APIC_clock()) {
982 /* No broadcast on UP ! */
983 if (num_possible_cpus() > 1)
984 setup_APIC_timer();
985 return;
989 * If nmi_watchdog is set to IO_APIC, we need the
990 * PIT/HPET going. Otherwise register lapic as a dummy
991 * device.
993 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
995 /* Setup the lapic or request the broadcast */
996 setup_APIC_timer();
997 amd_e400_c1e_apic_setup();
1000 void setup_secondary_APIC_clock(void)
1002 setup_APIC_timer();
1003 amd_e400_c1e_apic_setup();
1007 * The guts of the apic timer interrupt
1009 static void local_apic_timer_interrupt(void)
1011 struct clock_event_device *evt = this_cpu_ptr(&lapic_events);
1014 * Normally we should not be here till LAPIC has been initialized but
1015 * in some cases like kdump, its possible that there is a pending LAPIC
1016 * timer interrupt from previous kernel's context and is delivered in
1017 * new kernel the moment interrupts are enabled.
1019 * Interrupts are enabled early and LAPIC is setup much later, hence
1020 * its possible that when we get here evt->event_handler is NULL.
1021 * Check for event_handler being NULL and discard the interrupt as
1022 * spurious.
1024 if (!evt->event_handler) {
1025 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n",
1026 smp_processor_id());
1027 /* Switch it off */
1028 lapic_timer_shutdown(evt);
1029 return;
1033 * the NMI deadlock-detector uses this.
1035 inc_irq_stat(apic_timer_irqs);
1037 evt->event_handler(evt);
1041 * Local APIC timer interrupt. This is the most natural way for doing
1042 * local interrupts, but local timer interrupts can be emulated by
1043 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1045 * [ if a single-CPU system runs an SMP kernel then we call the local
1046 * interrupt as well. Thus we cannot inline the local irq ... ]
1048 __visible void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
1050 struct pt_regs *old_regs = set_irq_regs(regs);
1053 * NOTE! We'd better ACK the irq immediately,
1054 * because timer handling can be slow.
1056 * update_process_times() expects us to have done irq_enter().
1057 * Besides, if we don't timer interrupts ignore the global
1058 * interrupt lock, which is the WrongThing (tm) to do.
1060 entering_ack_irq();
1061 trace_local_timer_entry(LOCAL_TIMER_VECTOR);
1062 local_apic_timer_interrupt();
1063 trace_local_timer_exit(LOCAL_TIMER_VECTOR);
1064 exiting_irq();
1066 set_irq_regs(old_regs);
1069 int setup_profiling_timer(unsigned int multiplier)
1071 return -EINVAL;
1075 * Local APIC start and shutdown
1079 * clear_local_APIC - shutdown the local APIC
1081 * This is called, when a CPU is disabled and before rebooting, so the state of
1082 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
1083 * leftovers during boot.
1085 void clear_local_APIC(void)
1087 int maxlvt;
1088 u32 v;
1090 /* APIC hasn't been mapped yet */
1091 if (!x2apic_mode && !apic_phys)
1092 return;
1094 maxlvt = lapic_get_maxlvt();
1096 * Masking an LVT entry can trigger a local APIC error
1097 * if the vector is zero. Mask LVTERR first to prevent this.
1099 if (maxlvt >= 3) {
1100 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
1101 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
1104 * Careful: we have to set masks only first to deassert
1105 * any level-triggered sources.
1107 v = apic_read(APIC_LVTT);
1108 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
1109 v = apic_read(APIC_LVT0);
1110 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1111 v = apic_read(APIC_LVT1);
1112 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
1113 if (maxlvt >= 4) {
1114 v = apic_read(APIC_LVTPC);
1115 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
1118 /* lets not touch this if we didn't frob it */
1119 #ifdef CONFIG_X86_THERMAL_VECTOR
1120 if (maxlvt >= 5) {
1121 v = apic_read(APIC_LVTTHMR);
1122 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
1124 #endif
1125 #ifdef CONFIG_X86_MCE_INTEL
1126 if (maxlvt >= 6) {
1127 v = apic_read(APIC_LVTCMCI);
1128 if (!(v & APIC_LVT_MASKED))
1129 apic_write(APIC_LVTCMCI, v | APIC_LVT_MASKED);
1131 #endif
1134 * Clean APIC state for other OSs:
1136 apic_write(APIC_LVTT, APIC_LVT_MASKED);
1137 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1138 apic_write(APIC_LVT1, APIC_LVT_MASKED);
1139 if (maxlvt >= 3)
1140 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
1141 if (maxlvt >= 4)
1142 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
1144 /* Integrated APIC (!82489DX) ? */
1145 if (lapic_is_integrated()) {
1146 if (maxlvt > 3)
1147 /* Clear ESR due to Pentium errata 3AP and 11AP */
1148 apic_write(APIC_ESR, 0);
1149 apic_read(APIC_ESR);
1154 * disable_local_APIC - clear and disable the local APIC
1156 void disable_local_APIC(void)
1158 unsigned int value;
1160 /* APIC hasn't been mapped yet */
1161 if (!x2apic_mode && !apic_phys)
1162 return;
1164 clear_local_APIC();
1167 * Disable APIC (implies clearing of registers
1168 * for 82489DX!).
1170 value = apic_read(APIC_SPIV);
1171 value &= ~APIC_SPIV_APIC_ENABLED;
1172 apic_write(APIC_SPIV, value);
1174 #ifdef CONFIG_X86_32
1176 * When LAPIC was disabled by the BIOS and enabled by the kernel,
1177 * restore the disabled state.
1179 if (enabled_via_apicbase) {
1180 unsigned int l, h;
1182 rdmsr(MSR_IA32_APICBASE, l, h);
1183 l &= ~MSR_IA32_APICBASE_ENABLE;
1184 wrmsr(MSR_IA32_APICBASE, l, h);
1186 #endif
1190 * If Linux enabled the LAPIC against the BIOS default disable it down before
1191 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
1192 * not power-off. Additionally clear all LVT entries before disable_local_APIC
1193 * for the case where Linux didn't enable the LAPIC.
1195 void lapic_shutdown(void)
1197 unsigned long flags;
1199 if (!boot_cpu_has(X86_FEATURE_APIC) && !apic_from_smp_config())
1200 return;
1202 local_irq_save(flags);
1204 #ifdef CONFIG_X86_32
1205 if (!enabled_via_apicbase)
1206 clear_local_APIC();
1207 else
1208 #endif
1209 disable_local_APIC();
1212 local_irq_restore(flags);
1216 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1218 void __init sync_Arb_IDs(void)
1221 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1222 * needed on AMD.
1224 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1225 return;
1228 * Wait for idle.
1230 apic_wait_icr_idle();
1232 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
1233 apic_write(APIC_ICR, APIC_DEST_ALLINC |
1234 APIC_INT_LEVELTRIG | APIC_DM_INIT);
1238 * An initial setup of the virtual wire mode.
1240 void __init init_bsp_APIC(void)
1242 unsigned int value;
1245 * Don't do the setup now if we have a SMP BIOS as the
1246 * through-I/O-APIC virtual wire mode might be active.
1248 if (smp_found_config || !boot_cpu_has(X86_FEATURE_APIC))
1249 return;
1252 * Do not trust the local APIC being empty at bootup.
1254 clear_local_APIC();
1257 * Enable APIC.
1259 value = apic_read(APIC_SPIV);
1260 value &= ~APIC_VECTOR_MASK;
1261 value |= APIC_SPIV_APIC_ENABLED;
1263 #ifdef CONFIG_X86_32
1264 /* This bit is reserved on P4/Xeon and should be cleared */
1265 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1266 (boot_cpu_data.x86 == 15))
1267 value &= ~APIC_SPIV_FOCUS_DISABLED;
1268 else
1269 #endif
1270 value |= APIC_SPIV_FOCUS_DISABLED;
1271 value |= SPURIOUS_APIC_VECTOR;
1272 apic_write(APIC_SPIV, value);
1275 * Set up the virtual wire mode.
1277 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1278 value = APIC_DM_NMI;
1279 if (!lapic_is_integrated()) /* 82489DX */
1280 value |= APIC_LVT_LEVEL_TRIGGER;
1281 if (apic_extnmi == APIC_EXTNMI_NONE)
1282 value |= APIC_LVT_MASKED;
1283 apic_write(APIC_LVT1, value);
1286 static void lapic_setup_esr(void)
1288 unsigned int oldvalue, value, maxlvt;
1290 if (!lapic_is_integrated()) {
1291 pr_info("No ESR for 82489DX.\n");
1292 return;
1295 if (apic->disable_esr) {
1297 * Something untraceable is creating bad interrupts on
1298 * secondary quads ... for the moment, just leave the
1299 * ESR disabled - we can't do anything useful with the
1300 * errors anyway - mbligh
1302 pr_info("Leaving ESR disabled.\n");
1303 return;
1306 maxlvt = lapic_get_maxlvt();
1307 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1308 apic_write(APIC_ESR, 0);
1309 oldvalue = apic_read(APIC_ESR);
1311 /* enables sending errors */
1312 value = ERROR_APIC_VECTOR;
1313 apic_write(APIC_LVTERR, value);
1316 * spec says clear errors after enabling vector.
1318 if (maxlvt > 3)
1319 apic_write(APIC_ESR, 0);
1320 value = apic_read(APIC_ESR);
1321 if (value != oldvalue)
1322 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1323 "vector: 0x%08x after: 0x%08x\n",
1324 oldvalue, value);
1328 * setup_local_APIC - setup the local APIC
1330 * Used to setup local APIC while initializing BSP or bringing up APs.
1331 * Always called with preemption disabled.
1333 void setup_local_APIC(void)
1335 int cpu = smp_processor_id();
1336 unsigned int value, queued;
1337 int i, j, acked = 0;
1338 unsigned long long tsc = 0, ntsc;
1339 long long max_loops = cpu_khz ? cpu_khz : 1000000;
1341 if (boot_cpu_has(X86_FEATURE_TSC))
1342 tsc = rdtsc();
1344 if (disable_apic) {
1345 disable_ioapic_support();
1346 return;
1349 #ifdef CONFIG_X86_32
1350 /* Pound the ESR really hard over the head with a big hammer - mbligh */
1351 if (lapic_is_integrated() && apic->disable_esr) {
1352 apic_write(APIC_ESR, 0);
1353 apic_write(APIC_ESR, 0);
1354 apic_write(APIC_ESR, 0);
1355 apic_write(APIC_ESR, 0);
1357 #endif
1358 perf_events_lapic_init();
1361 * Double-check whether this APIC is really registered.
1362 * This is meaningless in clustered apic mode, so we skip it.
1364 BUG_ON(!apic->apic_id_registered());
1367 * Intel recommends to set DFR, LDR and TPR before enabling
1368 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1369 * document number 292116). So here it goes...
1371 apic->init_apic_ldr();
1373 #ifdef CONFIG_X86_32
1375 * APIC LDR is initialized. If logical_apicid mapping was
1376 * initialized during get_smp_config(), make sure it matches the
1377 * actual value.
1379 i = early_per_cpu(x86_cpu_to_logical_apicid, cpu);
1380 WARN_ON(i != BAD_APICID && i != logical_smp_processor_id());
1381 /* always use the value from LDR */
1382 early_per_cpu(x86_cpu_to_logical_apicid, cpu) =
1383 logical_smp_processor_id();
1384 #endif
1387 * Set Task Priority to 'accept all'. We never change this
1388 * later on.
1390 value = apic_read(APIC_TASKPRI);
1391 value &= ~APIC_TPRI_MASK;
1392 apic_write(APIC_TASKPRI, value);
1395 * After a crash, we no longer service the interrupts and a pending
1396 * interrupt from previous kernel might still have ISR bit set.
1398 * Most probably by now CPU has serviced that pending interrupt and
1399 * it might not have done the ack_APIC_irq() because it thought,
1400 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1401 * does not clear the ISR bit and cpu thinks it has already serivced
1402 * the interrupt. Hence a vector might get locked. It was noticed
1403 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1405 do {
1406 queued = 0;
1407 for (i = APIC_ISR_NR - 1; i >= 0; i--)
1408 queued |= apic_read(APIC_IRR + i*0x10);
1410 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1411 value = apic_read(APIC_ISR + i*0x10);
1412 for (j = 31; j >= 0; j--) {
1413 if (value & (1<<j)) {
1414 ack_APIC_irq();
1415 acked++;
1419 if (acked > 256) {
1420 printk(KERN_ERR "LAPIC pending interrupts after %d EOI\n",
1421 acked);
1422 break;
1424 if (queued) {
1425 if (boot_cpu_has(X86_FEATURE_TSC) && cpu_khz) {
1426 ntsc = rdtsc();
1427 max_loops = (cpu_khz << 10) - (ntsc - tsc);
1428 } else
1429 max_loops--;
1431 } while (queued && max_loops > 0);
1432 WARN_ON(max_loops <= 0);
1435 * Now that we are all set up, enable the APIC
1437 value = apic_read(APIC_SPIV);
1438 value &= ~APIC_VECTOR_MASK;
1440 * Enable APIC
1442 value |= APIC_SPIV_APIC_ENABLED;
1444 #ifdef CONFIG_X86_32
1446 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1447 * certain networking cards. If high frequency interrupts are
1448 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1449 * entry is masked/unmasked at a high rate as well then sooner or
1450 * later IOAPIC line gets 'stuck', no more interrupts are received
1451 * from the device. If focus CPU is disabled then the hang goes
1452 * away, oh well :-(
1454 * [ This bug can be reproduced easily with a level-triggered
1455 * PCI Ne2000 networking cards and PII/PIII processors, dual
1456 * BX chipset. ]
1459 * Actually disabling the focus CPU check just makes the hang less
1460 * frequent as it makes the interrupt distributon model be more
1461 * like LRU than MRU (the short-term load is more even across CPUs).
1465 * - enable focus processor (bit==0)
1466 * - 64bit mode always use processor focus
1467 * so no need to set it
1469 value &= ~APIC_SPIV_FOCUS_DISABLED;
1470 #endif
1473 * Set spurious IRQ vector
1475 value |= SPURIOUS_APIC_VECTOR;
1476 apic_write(APIC_SPIV, value);
1479 * Set up LVT0, LVT1:
1481 * set up through-local-APIC on the BP's LINT0. This is not
1482 * strictly necessary in pure symmetric-IO mode, but sometimes
1483 * we delegate interrupts to the 8259A.
1486 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1488 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1489 if (!cpu && (pic_mode || !value || skip_ioapic_setup)) {
1490 value = APIC_DM_EXTINT;
1491 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", cpu);
1492 } else {
1493 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1494 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", cpu);
1496 apic_write(APIC_LVT0, value);
1499 * Only the BSP sees the LINT1 NMI signal by default. This can be
1500 * modified by apic_extnmi= boot option.
1502 if ((!cpu && apic_extnmi != APIC_EXTNMI_NONE) ||
1503 apic_extnmi == APIC_EXTNMI_ALL)
1504 value = APIC_DM_NMI;
1505 else
1506 value = APIC_DM_NMI | APIC_LVT_MASKED;
1507 if (!lapic_is_integrated()) /* 82489DX */
1508 value |= APIC_LVT_LEVEL_TRIGGER;
1509 apic_write(APIC_LVT1, value);
1511 #ifdef CONFIG_X86_MCE_INTEL
1512 /* Recheck CMCI information after local APIC is up on CPU #0 */
1513 if (!cpu)
1514 cmci_recheck();
1515 #endif
1518 static void end_local_APIC_setup(void)
1520 lapic_setup_esr();
1522 #ifdef CONFIG_X86_32
1524 unsigned int value;
1525 /* Disable the local apic timer */
1526 value = apic_read(APIC_LVTT);
1527 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1528 apic_write(APIC_LVTT, value);
1530 #endif
1532 apic_pm_activate();
1536 * APIC setup function for application processors. Called from smpboot.c
1538 void apic_ap_setup(void)
1540 setup_local_APIC();
1541 end_local_APIC_setup();
1544 #ifdef CONFIG_X86_X2APIC
1545 int x2apic_mode;
1547 enum {
1548 X2APIC_OFF,
1549 X2APIC_ON,
1550 X2APIC_DISABLED,
1552 static int x2apic_state;
1554 static void __x2apic_disable(void)
1556 u64 msr;
1558 if (!boot_cpu_has(X86_FEATURE_APIC))
1559 return;
1561 rdmsrl(MSR_IA32_APICBASE, msr);
1562 if (!(msr & X2APIC_ENABLE))
1563 return;
1564 /* Disable xapic and x2apic first and then reenable xapic mode */
1565 wrmsrl(MSR_IA32_APICBASE, msr & ~(X2APIC_ENABLE | XAPIC_ENABLE));
1566 wrmsrl(MSR_IA32_APICBASE, msr & ~X2APIC_ENABLE);
1567 printk_once(KERN_INFO "x2apic disabled\n");
1570 static void __x2apic_enable(void)
1572 u64 msr;
1574 rdmsrl(MSR_IA32_APICBASE, msr);
1575 if (msr & X2APIC_ENABLE)
1576 return;
1577 wrmsrl(MSR_IA32_APICBASE, msr | X2APIC_ENABLE);
1578 printk_once(KERN_INFO "x2apic enabled\n");
1581 static int __init setup_nox2apic(char *str)
1583 if (x2apic_enabled()) {
1584 int apicid = native_apic_msr_read(APIC_ID);
1586 if (apicid >= 255) {
1587 pr_warning("Apicid: %08x, cannot enforce nox2apic\n",
1588 apicid);
1589 return 0;
1591 pr_warning("x2apic already enabled.\n");
1592 __x2apic_disable();
1594 setup_clear_cpu_cap(X86_FEATURE_X2APIC);
1595 x2apic_state = X2APIC_DISABLED;
1596 x2apic_mode = 0;
1597 return 0;
1599 early_param("nox2apic", setup_nox2apic);
1601 /* Called from cpu_init() to enable x2apic on (secondary) cpus */
1602 void x2apic_setup(void)
1605 * If x2apic is not in ON state, disable it if already enabled
1606 * from BIOS.
1608 if (x2apic_state != X2APIC_ON) {
1609 __x2apic_disable();
1610 return;
1612 __x2apic_enable();
1615 static __init void x2apic_disable(void)
1617 u32 x2apic_id, state = x2apic_state;
1619 x2apic_mode = 0;
1620 x2apic_state = X2APIC_DISABLED;
1622 if (state != X2APIC_ON)
1623 return;
1625 x2apic_id = read_apic_id();
1626 if (x2apic_id >= 255)
1627 panic("Cannot disable x2apic, id: %08x\n", x2apic_id);
1629 __x2apic_disable();
1630 register_lapic_address(mp_lapic_addr);
1633 static __init void x2apic_enable(void)
1635 if (x2apic_state != X2APIC_OFF)
1636 return;
1638 x2apic_mode = 1;
1639 x2apic_state = X2APIC_ON;
1640 __x2apic_enable();
1643 static __init void try_to_enable_x2apic(int remap_mode)
1645 if (x2apic_state == X2APIC_DISABLED)
1646 return;
1648 if (remap_mode != IRQ_REMAP_X2APIC_MODE) {
1649 /* IR is required if there is APIC ID > 255 even when running
1650 * under KVM
1652 if (max_physical_apicid > 255 ||
1653 !x86_init.hyper.x2apic_available()) {
1654 pr_info("x2apic: IRQ remapping doesn't support X2APIC mode\n");
1655 x2apic_disable();
1656 return;
1660 * without IR all CPUs can be addressed by IOAPIC/MSI
1661 * only in physical mode
1663 x2apic_phys = 1;
1665 x2apic_enable();
1668 void __init check_x2apic(void)
1670 if (x2apic_enabled()) {
1671 pr_info("x2apic: enabled by BIOS, switching to x2apic ops\n");
1672 x2apic_mode = 1;
1673 x2apic_state = X2APIC_ON;
1674 } else if (!boot_cpu_has(X86_FEATURE_X2APIC)) {
1675 x2apic_state = X2APIC_DISABLED;
1678 #else /* CONFIG_X86_X2APIC */
1679 static int __init validate_x2apic(void)
1681 if (!apic_is_x2apic_enabled())
1682 return 0;
1684 * Checkme: Can we simply turn off x2apic here instead of panic?
1686 panic("BIOS has enabled x2apic but kernel doesn't support x2apic, please disable x2apic in BIOS.\n");
1688 early_initcall(validate_x2apic);
1690 static inline void try_to_enable_x2apic(int remap_mode) { }
1691 static inline void __x2apic_enable(void) { }
1692 #endif /* !CONFIG_X86_X2APIC */
1694 void __init enable_IR_x2apic(void)
1696 unsigned long flags;
1697 int ret, ir_stat;
1699 if (skip_ioapic_setup) {
1700 pr_info("Not enabling interrupt remapping due to skipped IO-APIC setup\n");
1701 return;
1704 ir_stat = irq_remapping_prepare();
1705 if (ir_stat < 0 && !x2apic_supported())
1706 return;
1708 ret = save_ioapic_entries();
1709 if (ret) {
1710 pr_info("Saving IO-APIC state failed: %d\n", ret);
1711 return;
1714 local_irq_save(flags);
1715 legacy_pic->mask_all();
1716 mask_ioapic_entries();
1718 /* If irq_remapping_prepare() succeeded, try to enable it */
1719 if (ir_stat >= 0)
1720 ir_stat = irq_remapping_enable();
1721 /* ir_stat contains the remap mode or an error code */
1722 try_to_enable_x2apic(ir_stat);
1724 if (ir_stat < 0)
1725 restore_ioapic_entries();
1726 legacy_pic->restore_mask();
1727 local_irq_restore(flags);
1730 #ifdef CONFIG_X86_64
1732 * Detect and enable local APICs on non-SMP boards.
1733 * Original code written by Keir Fraser.
1734 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1735 * not correctly set up (usually the APIC timer won't work etc.)
1737 static int __init detect_init_APIC(void)
1739 if (!boot_cpu_has(X86_FEATURE_APIC)) {
1740 pr_info("No local APIC present\n");
1741 return -1;
1744 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1745 return 0;
1747 #else
1749 static int __init apic_verify(void)
1751 u32 features, h, l;
1754 * The APIC feature bit should now be enabled
1755 * in `cpuid'
1757 features = cpuid_edx(1);
1758 if (!(features & (1 << X86_FEATURE_APIC))) {
1759 pr_warning("Could not enable APIC!\n");
1760 return -1;
1762 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1763 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1765 /* The BIOS may have set up the APIC at some other address */
1766 if (boot_cpu_data.x86 >= 6) {
1767 rdmsr(MSR_IA32_APICBASE, l, h);
1768 if (l & MSR_IA32_APICBASE_ENABLE)
1769 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1772 pr_info("Found and enabled local APIC!\n");
1773 return 0;
1776 int __init apic_force_enable(unsigned long addr)
1778 u32 h, l;
1780 if (disable_apic)
1781 return -1;
1784 * Some BIOSes disable the local APIC in the APIC_BASE
1785 * MSR. This can only be done in software for Intel P6 or later
1786 * and AMD K7 (Model > 1) or later.
1788 if (boot_cpu_data.x86 >= 6) {
1789 rdmsr(MSR_IA32_APICBASE, l, h);
1790 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1791 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
1792 l &= ~MSR_IA32_APICBASE_BASE;
1793 l |= MSR_IA32_APICBASE_ENABLE | addr;
1794 wrmsr(MSR_IA32_APICBASE, l, h);
1795 enabled_via_apicbase = 1;
1798 return apic_verify();
1802 * Detect and initialize APIC
1804 static int __init detect_init_APIC(void)
1806 /* Disabled by kernel option? */
1807 if (disable_apic)
1808 return -1;
1810 switch (boot_cpu_data.x86_vendor) {
1811 case X86_VENDOR_AMD:
1812 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1813 (boot_cpu_data.x86 >= 15))
1814 break;
1815 goto no_apic;
1816 case X86_VENDOR_INTEL:
1817 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1818 (boot_cpu_data.x86 == 5 && boot_cpu_has(X86_FEATURE_APIC)))
1819 break;
1820 goto no_apic;
1821 default:
1822 goto no_apic;
1825 if (!boot_cpu_has(X86_FEATURE_APIC)) {
1827 * Over-ride BIOS and try to enable the local APIC only if
1828 * "lapic" specified.
1830 if (!force_enable_local_apic) {
1831 pr_info("Local APIC disabled by BIOS -- "
1832 "you can enable it with \"lapic\"\n");
1833 return -1;
1835 if (apic_force_enable(APIC_DEFAULT_PHYS_BASE))
1836 return -1;
1837 } else {
1838 if (apic_verify())
1839 return -1;
1842 apic_pm_activate();
1844 return 0;
1846 no_apic:
1847 pr_info("No local APIC present or hardware disabled\n");
1848 return -1;
1850 #endif
1853 * init_apic_mappings - initialize APIC mappings
1855 void __init init_apic_mappings(void)
1857 unsigned int new_apicid;
1859 apic_check_deadline_errata();
1861 if (x2apic_mode) {
1862 boot_cpu_physical_apicid = read_apic_id();
1863 return;
1866 /* If no local APIC can be found return early */
1867 if (!smp_found_config && detect_init_APIC()) {
1868 /* lets NOP'ify apic operations */
1869 pr_info("APIC: disable apic facility\n");
1870 apic_disable();
1871 } else {
1872 apic_phys = mp_lapic_addr;
1875 * If the system has ACPI MADT tables or MP info, the LAPIC
1876 * address is already registered.
1878 if (!acpi_lapic && !smp_found_config)
1879 register_lapic_address(apic_phys);
1883 * Fetch the APIC ID of the BSP in case we have a
1884 * default configuration (or the MP table is broken).
1886 new_apicid = read_apic_id();
1887 if (boot_cpu_physical_apicid != new_apicid) {
1888 boot_cpu_physical_apicid = new_apicid;
1890 * yeah -- we lie about apic_version
1891 * in case if apic was disabled via boot option
1892 * but it's not a problem for SMP compiled kernel
1893 * since smp_sanity_check is prepared for such a case
1894 * and disable smp mode
1896 boot_cpu_apic_version = GET_APIC_VERSION(apic_read(APIC_LVR));
1900 void __init register_lapic_address(unsigned long address)
1902 mp_lapic_addr = address;
1904 if (!x2apic_mode) {
1905 set_fixmap_nocache(FIX_APIC_BASE, address);
1906 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1907 APIC_BASE, address);
1909 if (boot_cpu_physical_apicid == -1U) {
1910 boot_cpu_physical_apicid = read_apic_id();
1911 boot_cpu_apic_version = GET_APIC_VERSION(apic_read(APIC_LVR));
1916 * Local APIC interrupts
1920 * This interrupt should _never_ happen with our APIC/SMP architecture
1922 __visible void __irq_entry smp_spurious_interrupt(struct pt_regs *regs)
1924 u8 vector = ~regs->orig_ax;
1925 u32 v;
1927 entering_irq();
1928 trace_spurious_apic_entry(vector);
1931 * Check if this really is a spurious interrupt and ACK it
1932 * if it is a vectored one. Just in case...
1933 * Spurious interrupts should not be ACKed.
1935 v = apic_read(APIC_ISR + ((vector & ~0x1f) >> 1));
1936 if (v & (1 << (vector & 0x1f)))
1937 ack_APIC_irq();
1939 inc_irq_stat(irq_spurious_count);
1941 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1942 pr_info("spurious APIC interrupt through vector %02x on CPU#%d, "
1943 "should never happen.\n", vector, smp_processor_id());
1945 trace_spurious_apic_exit(vector);
1946 exiting_irq();
1950 * This interrupt should never happen with our APIC/SMP architecture
1952 __visible void __irq_entry smp_error_interrupt(struct pt_regs *regs)
1954 static const char * const error_interrupt_reason[] = {
1955 "Send CS error", /* APIC Error Bit 0 */
1956 "Receive CS error", /* APIC Error Bit 1 */
1957 "Send accept error", /* APIC Error Bit 2 */
1958 "Receive accept error", /* APIC Error Bit 3 */
1959 "Redirectable IPI", /* APIC Error Bit 4 */
1960 "Send illegal vector", /* APIC Error Bit 5 */
1961 "Received illegal vector", /* APIC Error Bit 6 */
1962 "Illegal register address", /* APIC Error Bit 7 */
1964 u32 v, i = 0;
1966 entering_irq();
1967 trace_error_apic_entry(ERROR_APIC_VECTOR);
1969 /* First tickle the hardware, only then report what went on. -- REW */
1970 if (lapic_get_maxlvt() > 3) /* Due to the Pentium erratum 3AP. */
1971 apic_write(APIC_ESR, 0);
1972 v = apic_read(APIC_ESR);
1973 ack_APIC_irq();
1974 atomic_inc(&irq_err_count);
1976 apic_printk(APIC_DEBUG, KERN_DEBUG "APIC error on CPU%d: %02x",
1977 smp_processor_id(), v);
1979 v &= 0xff;
1980 while (v) {
1981 if (v & 0x1)
1982 apic_printk(APIC_DEBUG, KERN_CONT " : %s", error_interrupt_reason[i]);
1983 i++;
1984 v >>= 1;
1987 apic_printk(APIC_DEBUG, KERN_CONT "\n");
1989 trace_error_apic_exit(ERROR_APIC_VECTOR);
1990 exiting_irq();
1994 * connect_bsp_APIC - attach the APIC to the interrupt system
1996 static void __init connect_bsp_APIC(void)
1998 #ifdef CONFIG_X86_32
1999 if (pic_mode) {
2001 * Do not trust the local APIC being empty at bootup.
2003 clear_local_APIC();
2005 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
2006 * local APIC to INT and NMI lines.
2008 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
2009 "enabling APIC mode.\n");
2010 imcr_pic_to_apic();
2012 #endif
2016 * disconnect_bsp_APIC - detach the APIC from the interrupt system
2017 * @virt_wire_setup: indicates, whether virtual wire mode is selected
2019 * Virtual wire mode is necessary to deliver legacy interrupts even when the
2020 * APIC is disabled.
2022 void disconnect_bsp_APIC(int virt_wire_setup)
2024 unsigned int value;
2026 #ifdef CONFIG_X86_32
2027 if (pic_mode) {
2029 * Put the board back into PIC mode (has an effect only on
2030 * certain older boards). Note that APIC interrupts, including
2031 * IPIs, won't work beyond this point! The only exception are
2032 * INIT IPIs.
2034 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
2035 "entering PIC mode.\n");
2036 imcr_apic_to_pic();
2037 return;
2039 #endif
2041 /* Go back to Virtual Wire compatibility mode */
2043 /* For the spurious interrupt use vector F, and enable it */
2044 value = apic_read(APIC_SPIV);
2045 value &= ~APIC_VECTOR_MASK;
2046 value |= APIC_SPIV_APIC_ENABLED;
2047 value |= 0xf;
2048 apic_write(APIC_SPIV, value);
2050 if (!virt_wire_setup) {
2052 * For LVT0 make it edge triggered, active high,
2053 * external and enabled
2055 value = apic_read(APIC_LVT0);
2056 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
2057 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
2058 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
2059 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
2060 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
2061 apic_write(APIC_LVT0, value);
2062 } else {
2063 /* Disable LVT0 */
2064 apic_write(APIC_LVT0, APIC_LVT_MASKED);
2068 * For LVT1 make it edge triggered, active high,
2069 * nmi and enabled
2071 value = apic_read(APIC_LVT1);
2072 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
2073 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
2074 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
2075 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
2076 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
2077 apic_write(APIC_LVT1, value);
2081 * The number of allocated logical CPU IDs. Since logical CPU IDs are allocated
2082 * contiguously, it equals to current allocated max logical CPU ID plus 1.
2083 * All allocated CPU IDs should be in the [0, nr_logical_cpuids) range,
2084 * so the maximum of nr_logical_cpuids is nr_cpu_ids.
2086 * NOTE: Reserve 0 for BSP.
2088 static int nr_logical_cpuids = 1;
2091 * Used to store mapping between logical CPU IDs and APIC IDs.
2093 static int cpuid_to_apicid[] = {
2094 [0 ... NR_CPUS - 1] = -1,
2097 #ifdef CONFIG_SMP
2099 * apic_id_is_primary_thread - Check whether APIC ID belongs to a primary thread
2100 * @id: APIC ID to check
2102 bool apic_id_is_primary_thread(unsigned int apicid)
2104 u32 mask;
2106 if (smp_num_siblings == 1)
2107 return true;
2108 /* Isolate the SMT bit(s) in the APICID and check for 0 */
2109 mask = (1U << (fls(smp_num_siblings) - 1)) - 1;
2110 return !(apicid & mask);
2112 #endif
2115 * Should use this API to allocate logical CPU IDs to keep nr_logical_cpuids
2116 * and cpuid_to_apicid[] synchronized.
2118 static int allocate_logical_cpuid(int apicid)
2120 int i;
2123 * cpuid <-> apicid mapping is persistent, so when a cpu is up,
2124 * check if the kernel has allocated a cpuid for it.
2126 for (i = 0; i < nr_logical_cpuids; i++) {
2127 if (cpuid_to_apicid[i] == apicid)
2128 return i;
2131 /* Allocate a new cpuid. */
2132 if (nr_logical_cpuids >= nr_cpu_ids) {
2133 WARN_ONCE(1, "APIC: NR_CPUS/possible_cpus limit of %u reached. "
2134 "Processor %d/0x%x and the rest are ignored.\n",
2135 nr_cpu_ids, nr_logical_cpuids, apicid);
2136 return -EINVAL;
2139 cpuid_to_apicid[nr_logical_cpuids] = apicid;
2140 return nr_logical_cpuids++;
2143 int generic_processor_info(int apicid, int version)
2145 int cpu, max = nr_cpu_ids;
2146 bool boot_cpu_detected = physid_isset(boot_cpu_physical_apicid,
2147 phys_cpu_present_map);
2150 * boot_cpu_physical_apicid is designed to have the apicid
2151 * returned by read_apic_id(), i.e, the apicid of the
2152 * currently booting-up processor. However, on some platforms,
2153 * it is temporarily modified by the apicid reported as BSP
2154 * through MP table. Concretely:
2156 * - arch/x86/kernel/mpparse.c: MP_processor_info()
2157 * - arch/x86/mm/amdtopology.c: amd_numa_init()
2159 * This function is executed with the modified
2160 * boot_cpu_physical_apicid. So, disabled_cpu_apicid kernel
2161 * parameter doesn't work to disable APs on kdump 2nd kernel.
2163 * Since fixing handling of boot_cpu_physical_apicid requires
2164 * another discussion and tests on each platform, we leave it
2165 * for now and here we use read_apic_id() directly in this
2166 * function, generic_processor_info().
2168 if (disabled_cpu_apicid != BAD_APICID &&
2169 disabled_cpu_apicid != read_apic_id() &&
2170 disabled_cpu_apicid == apicid) {
2171 int thiscpu = num_processors + disabled_cpus;
2173 pr_warning("APIC: Disabling requested cpu."
2174 " Processor %d/0x%x ignored.\n",
2175 thiscpu, apicid);
2177 disabled_cpus++;
2178 return -ENODEV;
2182 * If boot cpu has not been detected yet, then only allow upto
2183 * nr_cpu_ids - 1 processors and keep one slot free for boot cpu
2185 if (!boot_cpu_detected && num_processors >= nr_cpu_ids - 1 &&
2186 apicid != boot_cpu_physical_apicid) {
2187 int thiscpu = max + disabled_cpus - 1;
2189 pr_warning(
2190 "APIC: NR_CPUS/possible_cpus limit of %i almost"
2191 " reached. Keeping one slot for boot cpu."
2192 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
2194 disabled_cpus++;
2195 return -ENODEV;
2198 if (num_processors >= nr_cpu_ids) {
2199 int thiscpu = max + disabled_cpus;
2201 pr_warning("APIC: NR_CPUS/possible_cpus limit of %i "
2202 "reached. Processor %d/0x%x ignored.\n",
2203 max, thiscpu, apicid);
2205 disabled_cpus++;
2206 return -EINVAL;
2209 if (apicid == boot_cpu_physical_apicid) {
2211 * x86_bios_cpu_apicid is required to have processors listed
2212 * in same order as logical cpu numbers. Hence the first
2213 * entry is BSP, and so on.
2214 * boot_cpu_init() already hold bit 0 in cpu_present_mask
2215 * for BSP.
2217 cpu = 0;
2219 /* Logical cpuid 0 is reserved for BSP. */
2220 cpuid_to_apicid[0] = apicid;
2221 } else {
2222 cpu = allocate_logical_cpuid(apicid);
2223 if (cpu < 0) {
2224 disabled_cpus++;
2225 return -EINVAL;
2230 * Validate version
2232 if (version == 0x0) {
2233 pr_warning("BIOS bug: APIC version is 0 for CPU %d/0x%x, fixing up to 0x10\n",
2234 cpu, apicid);
2235 version = 0x10;
2238 if (version != boot_cpu_apic_version) {
2239 pr_warning("BIOS bug: APIC version mismatch, boot CPU: %x, CPU %d: version %x\n",
2240 boot_cpu_apic_version, cpu, version);
2243 if (apicid > max_physical_apicid)
2244 max_physical_apicid = apicid;
2246 #if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
2247 early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
2248 early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
2249 #endif
2250 #ifdef CONFIG_X86_32
2251 early_per_cpu(x86_cpu_to_logical_apicid, cpu) =
2252 apic->x86_32_early_logical_apicid(cpu);
2253 #endif
2254 set_cpu_possible(cpu, true);
2255 physid_set(apicid, phys_cpu_present_map);
2256 set_cpu_present(cpu, true);
2257 num_processors++;
2259 return cpu;
2262 int hard_smp_processor_id(void)
2264 return read_apic_id();
2267 void default_init_apic_ldr(void)
2269 unsigned long val;
2271 apic_write(APIC_DFR, APIC_DFR_VALUE);
2272 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
2273 val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
2274 apic_write(APIC_LDR, val);
2277 int default_cpu_mask_to_apicid(const struct cpumask *mask,
2278 struct irq_data *irqdata,
2279 unsigned int *apicid)
2281 unsigned int cpu = cpumask_first(mask);
2283 if (cpu >= nr_cpu_ids)
2284 return -EINVAL;
2285 *apicid = per_cpu(x86_cpu_to_apicid, cpu);
2286 irq_data_update_effective_affinity(irqdata, cpumask_of(cpu));
2287 return 0;
2290 int flat_cpu_mask_to_apicid(const struct cpumask *mask,
2291 struct irq_data *irqdata,
2292 unsigned int *apicid)
2295 struct cpumask *effmsk = irq_data_get_effective_affinity_mask(irqdata);
2296 unsigned long cpu_mask = cpumask_bits(mask)[0] & APIC_ALL_CPUS;
2298 if (!cpu_mask)
2299 return -EINVAL;
2300 *apicid = (unsigned int)cpu_mask;
2301 cpumask_bits(effmsk)[0] = cpu_mask;
2302 return 0;
2306 * Override the generic EOI implementation with an optimized version.
2307 * Only called during early boot when only one CPU is active and with
2308 * interrupts disabled, so we know this does not race with actual APIC driver
2309 * use.
2311 void __init apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v))
2313 struct apic **drv;
2315 for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) {
2316 /* Should happen once for each apic */
2317 WARN_ON((*drv)->eoi_write == eoi_write);
2318 (*drv)->native_eoi_write = (*drv)->eoi_write;
2319 (*drv)->eoi_write = eoi_write;
2323 static void __init apic_bsp_up_setup(void)
2325 #ifdef CONFIG_X86_64
2326 apic_write(APIC_ID, apic->set_apic_id(boot_cpu_physical_apicid));
2327 #else
2329 * Hack: In case of kdump, after a crash, kernel might be booting
2330 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
2331 * might be zero if read from MP tables. Get it from LAPIC.
2333 # ifdef CONFIG_CRASH_DUMP
2334 boot_cpu_physical_apicid = read_apic_id();
2335 # endif
2336 #endif
2337 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
2341 * apic_bsp_setup - Setup function for local apic and io-apic
2342 * @upmode: Force UP mode (for APIC_init_uniprocessor)
2344 * Returns:
2345 * apic_id of BSP APIC
2347 int __init apic_bsp_setup(bool upmode)
2349 int id;
2351 connect_bsp_APIC();
2352 if (upmode)
2353 apic_bsp_up_setup();
2354 setup_local_APIC();
2356 if (x2apic_mode)
2357 id = apic_read(APIC_LDR);
2358 else
2359 id = GET_APIC_LOGICAL_ID(apic_read(APIC_LDR));
2361 enable_IO_APIC();
2362 end_local_APIC_setup();
2363 irq_remap_enable_fault_handling();
2364 setup_IO_APIC();
2365 /* Setup local timer */
2366 x86_init.timers.setup_percpu_clockev();
2367 return id;
2371 * This initializes the IO-APIC and APIC hardware if this is
2372 * a UP kernel.
2374 int __init APIC_init_uniprocessor(void)
2376 if (disable_apic) {
2377 pr_info("Apic disabled\n");
2378 return -1;
2380 #ifdef CONFIG_X86_64
2381 if (!boot_cpu_has(X86_FEATURE_APIC)) {
2382 disable_apic = 1;
2383 pr_info("Apic disabled by BIOS\n");
2384 return -1;
2386 #else
2387 if (!smp_found_config && !boot_cpu_has(X86_FEATURE_APIC))
2388 return -1;
2391 * Complain if the BIOS pretends there is one.
2393 if (!boot_cpu_has(X86_FEATURE_APIC) &&
2394 APIC_INTEGRATED(boot_cpu_apic_version)) {
2395 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
2396 boot_cpu_physical_apicid);
2397 return -1;
2399 #endif
2401 if (!smp_found_config)
2402 disable_ioapic_support();
2404 default_setup_apic_routing();
2405 apic_bsp_setup(true);
2406 return 0;
2409 #ifdef CONFIG_UP_LATE_INIT
2410 void __init up_late_init(void)
2412 APIC_init_uniprocessor();
2414 #endif
2417 * Power management
2419 #ifdef CONFIG_PM
2421 static struct {
2423 * 'active' is true if the local APIC was enabled by us and
2424 * not the BIOS; this signifies that we are also responsible
2425 * for disabling it before entering apm/acpi suspend
2427 int active;
2428 /* r/w apic fields */
2429 unsigned int apic_id;
2430 unsigned int apic_taskpri;
2431 unsigned int apic_ldr;
2432 unsigned int apic_dfr;
2433 unsigned int apic_spiv;
2434 unsigned int apic_lvtt;
2435 unsigned int apic_lvtpc;
2436 unsigned int apic_lvt0;
2437 unsigned int apic_lvt1;
2438 unsigned int apic_lvterr;
2439 unsigned int apic_tmict;
2440 unsigned int apic_tdcr;
2441 unsigned int apic_thmr;
2442 unsigned int apic_cmci;
2443 } apic_pm_state;
2445 static int lapic_suspend(void)
2447 unsigned long flags;
2448 int maxlvt;
2450 if (!apic_pm_state.active)
2451 return 0;
2453 maxlvt = lapic_get_maxlvt();
2455 apic_pm_state.apic_id = apic_read(APIC_ID);
2456 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
2457 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
2458 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
2459 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
2460 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
2461 if (maxlvt >= 4)
2462 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
2463 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
2464 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
2465 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
2466 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
2467 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
2468 #ifdef CONFIG_X86_THERMAL_VECTOR
2469 if (maxlvt >= 5)
2470 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
2471 #endif
2472 #ifdef CONFIG_X86_MCE_INTEL
2473 if (maxlvt >= 6)
2474 apic_pm_state.apic_cmci = apic_read(APIC_LVTCMCI);
2475 #endif
2477 local_irq_save(flags);
2478 disable_local_APIC();
2480 irq_remapping_disable();
2482 local_irq_restore(flags);
2483 return 0;
2486 static void lapic_resume(void)
2488 unsigned int l, h;
2489 unsigned long flags;
2490 int maxlvt;
2492 if (!apic_pm_state.active)
2493 return;
2495 local_irq_save(flags);
2498 * IO-APIC and PIC have their own resume routines.
2499 * We just mask them here to make sure the interrupt
2500 * subsystem is completely quiet while we enable x2apic
2501 * and interrupt-remapping.
2503 mask_ioapic_entries();
2504 legacy_pic->mask_all();
2506 if (x2apic_mode) {
2507 __x2apic_enable();
2508 } else {
2510 * Make sure the APICBASE points to the right address
2512 * FIXME! This will be wrong if we ever support suspend on
2513 * SMP! We'll need to do this as part of the CPU restore!
2515 if (boot_cpu_data.x86 >= 6) {
2516 rdmsr(MSR_IA32_APICBASE, l, h);
2517 l &= ~MSR_IA32_APICBASE_BASE;
2518 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2519 wrmsr(MSR_IA32_APICBASE, l, h);
2523 maxlvt = lapic_get_maxlvt();
2524 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2525 apic_write(APIC_ID, apic_pm_state.apic_id);
2526 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2527 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2528 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2529 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2530 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2531 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
2532 #ifdef CONFIG_X86_THERMAL_VECTOR
2533 if (maxlvt >= 5)
2534 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2535 #endif
2536 #ifdef CONFIG_X86_MCE_INTEL
2537 if (maxlvt >= 6)
2538 apic_write(APIC_LVTCMCI, apic_pm_state.apic_cmci);
2539 #endif
2540 if (maxlvt >= 4)
2541 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
2542 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2543 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2544 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2545 apic_write(APIC_ESR, 0);
2546 apic_read(APIC_ESR);
2547 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2548 apic_write(APIC_ESR, 0);
2549 apic_read(APIC_ESR);
2551 irq_remapping_reenable(x2apic_mode);
2553 local_irq_restore(flags);
2557 * This device has no shutdown method - fully functioning local APICs
2558 * are needed on every CPU up until machine_halt/restart/poweroff.
2561 static struct syscore_ops lapic_syscore_ops = {
2562 .resume = lapic_resume,
2563 .suspend = lapic_suspend,
2566 static void apic_pm_activate(void)
2568 apic_pm_state.active = 1;
2571 static int __init init_lapic_sysfs(void)
2573 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
2574 if (boot_cpu_has(X86_FEATURE_APIC))
2575 register_syscore_ops(&lapic_syscore_ops);
2577 return 0;
2580 /* local apic needs to resume before other devices access its registers. */
2581 core_initcall(init_lapic_sysfs);
2583 #else /* CONFIG_PM */
2585 static void apic_pm_activate(void) { }
2587 #endif /* CONFIG_PM */
2589 #ifdef CONFIG_X86_64
2591 static int multi_checked;
2592 static int multi;
2594 static int set_multi(const struct dmi_system_id *d)
2596 if (multi)
2597 return 0;
2598 pr_info("APIC: %s detected, Multi Chassis\n", d->ident);
2599 multi = 1;
2600 return 0;
2603 static const struct dmi_system_id multi_dmi_table[] = {
2605 .callback = set_multi,
2606 .ident = "IBM System Summit2",
2607 .matches = {
2608 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
2609 DMI_MATCH(DMI_PRODUCT_NAME, "Summit2"),
2615 static void dmi_check_multi(void)
2617 if (multi_checked)
2618 return;
2620 dmi_check_system(multi_dmi_table);
2621 multi_checked = 1;
2625 * apic_is_clustered_box() -- Check if we can expect good TSC
2627 * Thus far, the major user of this is IBM's Summit2 series:
2628 * Clustered boxes may have unsynced TSC problems if they are
2629 * multi-chassis.
2630 * Use DMI to check them
2632 int apic_is_clustered_box(void)
2634 dmi_check_multi();
2635 return multi;
2637 #endif
2640 * APIC command line parameters
2642 static int __init setup_disableapic(char *arg)
2644 disable_apic = 1;
2645 setup_clear_cpu_cap(X86_FEATURE_APIC);
2646 return 0;
2648 early_param("disableapic", setup_disableapic);
2650 /* same as disableapic, for compatibility */
2651 static int __init setup_nolapic(char *arg)
2653 return setup_disableapic(arg);
2655 early_param("nolapic", setup_nolapic);
2657 static int __init parse_lapic_timer_c2_ok(char *arg)
2659 local_apic_timer_c2_ok = 1;
2660 return 0;
2662 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2664 static int __init parse_disable_apic_timer(char *arg)
2666 disable_apic_timer = 1;
2667 return 0;
2669 early_param("noapictimer", parse_disable_apic_timer);
2671 static int __init parse_nolapic_timer(char *arg)
2673 disable_apic_timer = 1;
2674 return 0;
2676 early_param("nolapic_timer", parse_nolapic_timer);
2678 static int __init apic_set_verbosity(char *arg)
2680 if (!arg) {
2681 #ifdef CONFIG_X86_64
2682 skip_ioapic_setup = 0;
2683 return 0;
2684 #endif
2685 return -EINVAL;
2688 if (strcmp("debug", arg) == 0)
2689 apic_verbosity = APIC_DEBUG;
2690 else if (strcmp("verbose", arg) == 0)
2691 apic_verbosity = APIC_VERBOSE;
2692 else {
2693 pr_warning("APIC Verbosity level %s not recognised"
2694 " use apic=verbose or apic=debug\n", arg);
2695 return -EINVAL;
2698 return 0;
2700 early_param("apic", apic_set_verbosity);
2702 static int __init lapic_insert_resource(void)
2704 if (!apic_phys)
2705 return -1;
2707 /* Put local APIC into the resource map. */
2708 lapic_resource.start = apic_phys;
2709 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2710 insert_resource(&iomem_resource, &lapic_resource);
2712 return 0;
2716 * need call insert after e820__reserve_resources()
2717 * that is using request_resource
2719 late_initcall(lapic_insert_resource);
2721 static int __init apic_set_disabled_cpu_apicid(char *arg)
2723 if (!arg || !get_option(&arg, &disabled_cpu_apicid))
2724 return -EINVAL;
2726 return 0;
2728 early_param("disable_cpu_apicid", apic_set_disabled_cpu_apicid);
2730 static int __init apic_set_extnmi(char *arg)
2732 if (!arg)
2733 return -EINVAL;
2735 if (!strncmp("all", arg, 3))
2736 apic_extnmi = APIC_EXTNMI_ALL;
2737 else if (!strncmp("none", arg, 4))
2738 apic_extnmi = APIC_EXTNMI_NONE;
2739 else if (!strncmp("bsp", arg, 3))
2740 apic_extnmi = APIC_EXTNMI_BSP;
2741 else {
2742 pr_warn("Unknown external NMI delivery mode `%s' ignored\n", arg);
2743 return -EINVAL;
2746 return 0;
2748 early_param("apic_extnmi", apic_set_extnmi);