17 #include <linux/perf_event.h>
26 #include <linux/module.h>
29 #include <linux/timex.h>
38 #include <asm/irq_remapping.h>
39 #include <asm/perf_event.h>
41 #include <asm/pgalloc.h>
44 #include <asm/i8259.h>
56 #include <asm/hypervisor.h>
104 force_enable_local_apic = 1;
109 static int enabled_via_apicbase;
119 static inline void imcr_pic_to_apic(
void)
127 static inline void imcr_apic_to_pic(
void)
138 static __init int setup_apicpmtimer(
char *
s)
140 apic_calibrate_pmtmr = 1;
144 __setup(
"apicpmtimer", setup_apicpmtimer);
148 #ifdef CONFIG_X86_X2APIC
150 int x2apic_preenabled;
151 static int x2apic_disabled;
153 static __init int setup_nox2apic(
char *
str)
155 if (x2apic_enabled()) {
159 pr_warning(
"Apicid: %08x, cannot enforce nox2apic\n",
164 pr_warning(
"x2apic already enabled. will disable it\n");
195 static struct resource lapic_resource = {
196 .name =
"Local APIC",
202 static void apic_pm_activate(
void);
204 static unsigned long apic_phys;
209 static inline int lapic_get_version(
void)
217 static inline int lapic_is_integrated(
void)
229 static int modern_apic(
void)
235 return lapic_get_version() >= 0x14;
242 static void __init apic_disable(
void)
244 pr_info(
"APIC: switched to apic NOOP\n");
266 }
while (timeout++ < 1000);
284 return icr1 | ((
u64)icr2 << 32);
291 int get_physical_broadcast(
void)
293 return modern_apic() ? 0xff : 0xf;
317 #define APIC_DIVISOR 16
329 static void __setup_APIC_LVTT(
unsigned int clocks,
int oneshot,
int irqen)
331 unsigned int lvtt_value, tmp_value;
336 if (!lapic_is_integrated())
378 static inline int eilvt_entry_is_changeable(
unsigned int old,
unsigned int new)
382 || ((
new & ~APIC_EILVT_MASKED) == old);
385 static unsigned int reserve_eilvt_offset(
int offset,
unsigned int new)
394 vector = rsvd & ~APIC_EILVT_MASKED;
395 if (vector && !eilvt_entry_is_changeable(vector,
new))
399 }
while (rsvd !=
new);
401 rsvd &= ~APIC_EILVT_MASKED;
402 if (rsvd && rsvd != vector)
403 pr_info(
"LVT offset %d assigned for vector 0x%02x\n",
420 new = (mask << 16) | (msg_type << 8) |
vector;
421 old = apic_read(reg);
422 reserved = reserve_eilvt_offset(offset,
new);
424 if (reserved !=
new) {
425 pr_err(
FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
426 "vector 0x%x, but the register is already in use for "
427 "vector 0x%x on another cpu\n",
432 if (!eilvt_entry_is_changeable(old,
new)) {
433 pr_err(
FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
434 "vector 0x%x, but the register is already in use for "
435 "vector 0x%x on this cpu\n",
440 apic_write(reg,
new);
449 static int lapic_next_event(
unsigned long delta,
450 struct clock_event_device *
evt)
459 static void lapic_timer_setup(
enum clock_event_mode
mode,
460 struct clock_event_device *
evt)
466 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
472 case CLOCK_EVT_MODE_PERIODIC:
473 case CLOCK_EVT_MODE_ONESHOT:
475 mode != CLOCK_EVT_MODE_PERIODIC, 1);
477 case CLOCK_EVT_MODE_UNUSED:
478 case CLOCK_EVT_MODE_SHUTDOWN:
484 case CLOCK_EVT_MODE_RESUME:
495 static void lapic_timer_broadcast(
const struct cpumask *
mask)
506 static struct clock_event_device lapic_clockevent = {
508 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
509 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
511 .set_mode = lapic_timer_setup,
512 .set_next_event = lapic_next_event,
513 .broadcast = lapic_timer_broadcast,
523 static void __cpuinit setup_APIC_timer(
void)
525 struct clock_event_device *levt = &
__get_cpu_var(lapic_events);
528 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_C3STOP;
530 lapic_clockevent.rating = 150;
533 memcpy(levt, &lapic_clockevent,
sizeof(*levt));
560 #define LAPIC_CAL_LOOPS (HZ/10)
563 static __initdata long lapic_cal_t1, lapic_cal_t2;
564 static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
565 static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
566 static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
571 static void __init lapic_cal_handler(
struct clock_event_device *
dev)
573 unsigned long long tsc = 0;
575 unsigned long pm = acpi_pm_read_early();
580 switch (lapic_cal_loops++) {
582 lapic_cal_t1 = tapic;
583 lapic_cal_tsc1 =
tsc;
589 lapic_cal_t2 = tapic;
590 lapic_cal_tsc2 =
tsc;
591 if (pm < lapic_cal_pm1)
600 calibrate_by_pmtimer(
long deltapm,
long *
delta,
long *deltatsc)
603 const long pm_thresh = pm_100ms / 100;
607 #ifndef CONFIG_X86_PM_TIMER
619 if (deltapm > (pm_100ms - pm_thresh) &&
620 deltapm < (pm_100ms + pm_thresh)) {
625 res = (((
u64)deltapm) *
mult) >> 22;
628 "with PM-Timer: %ldms instead of 100ms\n",(
long)res);
631 res = (((
u64)(*delta)) * pm_100ms);
633 pr_info(
"APIC delta adjusted to PM-Timer: "
634 "%lu (%ld)\n", (
unsigned long)res, *delta);
639 res = (((
u64)(*deltatsc)) * pm_100ms);
642 "PM-Timer: %lu (%ld)\n",
643 (
unsigned long)res, *deltatsc);
644 *deltatsc = (
long)res;
650 static int __init calibrate_APIC_clock(
void)
652 struct clock_event_device *levt = &
__get_cpu_var(lapic_events);
653 void (*real_handler)(
struct clock_event_device *
dev);
654 unsigned long deltaj;
655 long delta, deltatsc;
656 int pm_referenced = 0;
669 lapic_clockevent.max_delta_ns =
671 lapic_clockevent.min_delta_ns =
673 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
687 __setup_APIC_LVTT(0xffffffff, 0, 0);
701 delta = lapic_cal_t1 - lapic_cal_t2;
704 deltatsc = (
long)(lapic_cal_tsc2 - lapic_cal_tsc1);
707 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
712 lapic_clockevent.shift);
713 lapic_clockevent.max_delta_ns =
715 lapic_clockevent.min_delta_ns =
742 pr_warning(
"APIC frequency too slow, disabling apic timer\n");
746 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
752 if (!pm_referenced) {
758 levt->event_handler = lapic_cal_handler;
759 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
760 lapic_cal_loops = -1;
769 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
772 deltaj = lapic_cal_j2 - lapic_cal_j1;
779 levt->features |= CLOCK_EVT_FEAT_DUMMY;
783 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
784 pr_warning(
"APIC timer disabled due to verification failure\n");
804 if (disable_apic_timer) {
805 pr_info(
"Disabling APIC timer\n");
808 lapic_clockevent.mult = 1;
815 "calibrating APIC timer ...\n");
817 if (calibrate_APIC_clock()) {
829 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
843 static void local_apic_timer_interrupt(
void)
846 struct clock_event_device *evt = &
per_cpu(lapic_events, cpu);
859 if (!evt->event_handler) {
860 pr_warning(
"Spurious LAPIC timer interrupt on cpu %d\n", cpu);
862 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
871 evt->event_handler(evt);
884 struct pt_regs *old_regs = set_irq_regs(regs);
898 local_apic_timer_interrupt();
901 set_irq_regs(old_regs);
954 #ifdef CONFIG_X86_THERMAL_VECTOR
960 #ifdef CONFIG_X86_MCE_INTEL
980 if (lapic_is_integrated()) {
1009 #ifdef CONFIG_X86_32
1014 if (enabled_via_apicbase) {
1032 unsigned long flags;
1034 if (!cpu_has_apic && !apic_from_smp_config())
1039 #ifdef CONFIG_X86_32
1040 if (!enabled_via_apicbase)
1080 if (reg1 == 0x00 || reg1 == 0xff)
1083 if (reg1 < 0x02 || reg1 == 0xff)
1126 apic_wait_icr_idle();
1159 #ifdef CONFIG_X86_32
1175 if (!lapic_is_integrated())
1180 static void __cpuinit lapic_setup_esr(
void)
1182 unsigned int oldvalue,
value, maxlvt;
1184 if (!lapic_is_integrated()) {
1185 pr_info(
"No ESR for 82489DX.\n");
1196 pr_info(
"Leaving ESR disabled.\n");
1215 if (value != oldvalue)
1217 "vector: 0x%08x after: 0x%08x\n",
1230 unsigned int value, queued;
1231 int i,
j, acked = 0;
1232 unsigned long long tsc = 0,
ntsc;
1233 long long max_loops =
cpu_khz;
1243 #ifdef CONFIG_X86_32
1267 #ifdef CONFIG_X86_32
1277 logical_smp_processor_id();
1286 if (
apic->x86_32_numa_cpu_node)
1288 apic->x86_32_numa_cpu_node(cpu));
1313 queued |= apic_read(
APIC_IRR + i*0x10);
1316 value = apic_read(
APIC_ISR + i*0x10);
1317 for (j = 31; j >= 0; j--) {
1318 if (value & (1<<j)) {
1336 }
while (queued && max_loops > 0);
1349 #ifdef CONFIG_X86_32
1395 if (!cpu && (
pic_mode || !value)) {
1411 if (!lapic_is_integrated())
1415 #ifdef CONFIG_X86_MCE_INTEL
1426 #ifdef CONFIG_X86_32
1452 #ifdef CONFIG_X86_X2APIC
1456 static inline void __disable_x2apic(
u64 msr)
1463 static __init void disable_x2apic(
void)
1472 u32 x2apic_id = read_apic_id();
1474 if (x2apic_id >= 255)
1475 panic(
"Cannot disable x2apic, id: %08x\n", x2apic_id);
1477 pr_info(
"Disabling x2apic\n");
1478 __disable_x2apic(msr);
1485 x2apic_disabled = 1;
1492 void check_x2apic(
void)
1494 if (x2apic_enabled()) {
1495 pr_info(
"x2apic enabled by BIOS, switching to x2apic ops\n");
1500 void enable_x2apic(
void)
1505 if (x2apic_disabled) {
1506 __disable_x2apic(msr);
1513 if (!(msr & X2APIC_ENABLE)) {
1522 #ifdef CONFIG_IRQ_REMAP
1524 pr_debug(
"intr-remapping not supported\n");
1529 pr_info(
"Skipped enabling intr-remap because of skipping "
1541 unsigned long flags;
1542 int ret, x2apic_enabled = 0;
1543 int hardware_init_ret;
1549 if (hardware_init_ret && !x2apic_supported())
1554 pr_info(
"Saving IO-APIC state failed: %d\n", ret);
1562 if (x2apic_preenabled && nox2apic)
1565 if (hardware_init_ret)
1570 if (!x2apic_supported())
1578 !hypervisor_x2apic_available()) {
1579 if (x2apic_preenabled)
1587 x2apic_force_phys();
1591 pr_info(
"x2apic not enabled, IRQ remapping is in xapic mode\n");
1610 #ifdef CONFIG_X86_64
1617 static int __init detect_init_APIC(
void)
1619 if (!cpu_has_apic) {
1620 pr_info(
"No local APIC present\n");
1629 static int __init apic_verify(
void)
1637 features = cpuid_edx(1);
1652 pr_info(
"Found and enabled local APIC!\n");
1671 pr_info(
"Local APIC disabled by BIOS -- reenabling.\n");
1673 l |= MSR_IA32_APICBASE_ENABLE |
addr;
1675 enabled_via_apicbase = 1;
1678 return apic_verify();
1684 static int __init detect_init_APIC(
void)
1705 if (!cpu_has_apic) {
1710 if (!force_enable_local_apic) {
1711 pr_info(
"Local APIC disabled by BIOS -- "
1712 "you can enable it with \"lapic\"\n");
1727 pr_info(
"No local APIC present or hardware disabled\n");
1737 unsigned int new_apicid;
1747 pr_info(
"APIC: disable apic facility\n");
1764 new_apicid = read_apic_id();
1807 #ifdef CONFIG_X86_64
1808 if (!cpu_has_apic) {
1810 pr_info(
"Apic disabled by BIOS\n");
1820 if (!cpu_has_apic &&
1822 pr_err(
"BIOS bug, local APIC 0x%x not detected!...\n",
1823 boot_cpu_physical_apicid);
1833 #ifdef CONFIG_X86_64
1834 apic_write(
APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
1841 # ifdef CONFIG_CRASH_DUMP
1842 boot_cpu_physical_apicid = read_apic_id();
1845 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
1848 #ifdef CONFIG_X86_IO_APIC
1859 #ifdef CONFIG_X86_IO_APIC
1867 x86_init.timers.setup_percpu_clockev();
1896 pr_info(
"spurious APIC interrupt on CPU#%d, "
1908 static const char *
const error_interrupt_reason[] = {
1911 "Send accept error",
1912 "Receive accept error",
1914 "Send illegal vector",
1915 "Received illegal vector",
1916 "Illegal register address",
1949 #ifdef CONFIG_X86_32
1960 "enabling APIC mode.\n");
1979 #ifdef CONFIG_X86_32
1988 "entering PIC mode.\n");
2003 if (!virt_wire_setup) {
2037 phys_cpu_present_map);
2045 int thiscpu = max + disabled_cpus - 1;
2048 "ACPI: NR_CPUS/possible_cpus limit of %i almost"
2049 " reached. Keeping one slot for boot cpu."
2050 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
2057 int thiscpu = max + disabled_cpus;
2060 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
2061 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
2083 if (version == 0x0) {
2084 pr_warning(
"BIOS bug: APIC version is 0 for CPU %d/0x%x, fixing up to 0x10\n",
2091 pr_warning(
"BIOS bug: APIC version mismatch, boot CPU: %x, CPU %d: version %x\n",
2092 apic_version[boot_cpu_physical_apicid], cpu, version);
2099 #if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
2103 #ifdef CONFIG_X86_32
2105 apic->x86_32_early_logical_apicid(cpu);
2113 return read_apic_id();
2120 apic_write(
APIC_DFR, APIC_DFR_VALUE);
2127 const struct cpumask *andmask,
2138 *apicid =
per_cpu(x86_cpu_to_apicid, cpu);
2175 unsigned int apic_id;
2176 unsigned int apic_taskpri;
2177 unsigned int apic_ldr;
2178 unsigned int apic_dfr;
2179 unsigned int apic_spiv;
2180 unsigned int apic_lvtt;
2181 unsigned int apic_lvtpc;
2182 unsigned int apic_lvt0;
2183 unsigned int apic_lvt1;
2184 unsigned int apic_lvterr;
2185 unsigned int apic_tmict;
2186 unsigned int apic_tdcr;
2187 unsigned int apic_thmr;
2190 static int lapic_suspend(
void)
2192 unsigned long flags;
2195 if (!apic_pm_state.active)
2200 apic_pm_state.apic_id = apic_read(
APIC_ID);
2202 apic_pm_state.apic_ldr = apic_read(
APIC_LDR);
2203 apic_pm_state.apic_dfr = apic_read(
APIC_DFR);
2204 apic_pm_state.apic_spiv = apic_read(
APIC_SPIV);
2205 apic_pm_state.apic_lvtt = apic_read(
APIC_LVTT);
2207 apic_pm_state.apic_lvtpc = apic_read(
APIC_LVTPC);
2208 apic_pm_state.apic_lvt0 = apic_read(
APIC_LVT0);
2209 apic_pm_state.apic_lvt1 = apic_read(
APIC_LVT1);
2210 apic_pm_state.apic_lvterr = apic_read(
APIC_LVTERR);
2211 apic_pm_state.apic_tmict = apic_read(
APIC_TMICT);
2212 apic_pm_state.apic_tdcr = apic_read(
APIC_TDCR);
2213 #ifdef CONFIG_X86_THERMAL_VECTOR
2228 static void lapic_resume(
void)
2231 unsigned long flags;
2234 if (!apic_pm_state.active)
2268 apic_write(
APIC_ID, apic_pm_state.apic_id);
2269 apic_write(
APIC_DFR, apic_pm_state.apic_dfr);
2270 apic_write(
APIC_LDR, apic_pm_state.apic_ldr);
2272 apic_write(
APIC_SPIV, apic_pm_state.apic_spiv);
2273 apic_write(
APIC_LVT0, apic_pm_state.apic_lvt0);
2274 apic_write(
APIC_LVT1, apic_pm_state.apic_lvt1);
2275 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
2280 apic_write(
APIC_LVTPC, apic_pm_state.apic_lvtpc);
2281 apic_write(
APIC_LVTT, apic_pm_state.apic_lvtt);
2282 apic_write(
APIC_TDCR, apic_pm_state.apic_tdcr);
2283 apic_write(
APIC_TMICT, apic_pm_state.apic_tmict);
2286 apic_write(
APIC_LVTERR, apic_pm_state.apic_lvterr);
2303 .suspend = lapic_suspend,
2306 static void __cpuinit apic_pm_activate(
void)
2308 apic_pm_state.active = 1;
2311 static int __init init_lapic_sysfs(
void)
2325 static void apic_pm_activate(
void) { }
2329 #ifdef CONFIG_X86_64
2331 static int __cpuinit apic_cluster_num(
void)
2335 u16 *bios_cpu_apicid;
2343 if (bios_cpu_apicid) {
2344 id = bios_cpu_apicid[
i];
2345 }
else if (i < nr_cpu_ids) {
2347 id =
per_cpu(x86_bios_cpu_apicid, i);
2367 clusters += 1 +
zeros;
2383 pr_info(
"APIC: %s detected, Multi Chassis\n", d->
ident);
2391 .ident =
"IBM System Summit2",
2400 static void __cpuinit dmi_check_multi(
void)
2417 __cpuinit int apic_is_clustered_box(
void)
2430 if (apic_cluster_num() > 1)
2440 static int __init setup_disableapic(
char *
arg)
2449 static int __init setup_nolapic(
char *
arg)
2451 return setup_disableapic(arg);
2455 static int __init parse_lapic_timer_c2_ok(
char *
arg)
2460 early_param(
"lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2462 static int __init parse_disable_apic_timer(
char *
arg)
2464 disable_apic_timer = 1;
2467 early_param(
"noapictimer", parse_disable_apic_timer);
2469 static int __init parse_nolapic_timer(
char *
arg)
2471 disable_apic_timer = 1;
2474 early_param(
"nolapic_timer", parse_nolapic_timer);
2476 static int __init apic_set_verbosity(
char *
arg)
2479 #ifdef CONFIG_X86_64
2486 if (
strcmp(
"debug", arg) == 0)
2488 else if (
strcmp(
"verbose", arg) == 0)
2491 pr_warning(
"APIC Verbosity level %s not recognised"
2492 " use apic=verbose or apic=debug\n", arg);
2500 static int __init lapic_insert_resource(
void)
2506 lapic_resource.
start = apic_phys;