linux/arch/i386/kernel/apic.c
<<
>>
Prefs
   1/*
   2 *      Local APIC handling, local APIC timers
   3 *
   4 *      (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
   5 *
   6 *      Fixes
   7 *      Maciej W. Rozycki       :       Bits for genuine 82489DX APICs;
   8 *                                      thanks to Eric Gilmore
   9 *                                      and Rolf G. Tews
  10 *                                      for testing these extensively.
  11 *      Maciej W. Rozycki       :       Various updates and fixes.
  12 *      Mikael Pettersson       :       Power Management for UP-APIC.
  13 *      Pavel Machek and
  14 *      Mikael Pettersson       :       PM converted to driver model.
  15 */
  16
  17#include <linux/init.h>
  18
  19#include <linux/mm.h>
  20#include <linux/delay.h>
  21#include <linux/bootmem.h>
  22#include <linux/interrupt.h>
  23#include <linux/mc146818rtc.h>
  24#include <linux/kernel_stat.h>
  25#include <linux/sysdev.h>
  26#include <linux/cpu.h>
  27#include <linux/clockchips.h>
  28#include <linux/acpi_pmtmr.h>
  29#include <linux/module.h>
  30#include <linux/dmi.h>
  31
  32#include <asm/atomic.h>
  33#include <asm/smp.h>
  34#include <asm/mtrr.h>
  35#include <asm/mpspec.h>
  36#include <asm/desc.h>
  37#include <asm/arch_hooks.h>
  38#include <asm/hpet.h>
  39#include <asm/i8253.h>
  40#include <asm/nmi.h>
  41
  42#include <mach_apic.h>
  43#include <mach_apicdef.h>
  44#include <mach_ipi.h>
  45
  46#include "io_ports.h"
  47
  48/*
  49 * Sanity check
  50 */
  51#if (SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F
  52# error SPURIOUS_APIC_VECTOR definition error
  53#endif
  54
  55/*
  56 * Knob to control our willingness to enable the local APIC.
  57 *
  58 * -1=force-disable, +1=force-enable
  59 */
  60static int enable_local_apic __initdata = 0;
  61
  62/* Local APIC timer verification ok */
  63static int local_apic_timer_verify_ok;
  64/* Disable local APIC timer from the kernel commandline or via dmi quirk
  65   or using CPU MSR check */
  66int local_apic_timer_disabled;
  67/* Local APIC timer works in C2 */
  68int local_apic_timer_c2_ok;
  69EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
  70
  71/*
  72 * Debug level, exported for io_apic.c
  73 */
  74int apic_verbosity;
  75
  76static unsigned int calibration_result;
  77
  78static int lapic_next_event(unsigned long delta,
  79                            struct clock_event_device *evt);
  80static void lapic_timer_setup(enum clock_event_mode mode,
  81                              struct clock_event_device *evt);
  82static void lapic_timer_broadcast(cpumask_t mask);
  83static void apic_pm_activate(void);
  84
  85/*
  86 * The local apic timer can be used for any function which is CPU local.
  87 */
  88static struct clock_event_device lapic_clockevent = {
  89        .name           = "lapic",
  90        .features       = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
  91                        | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
  92        .shift          = 32,
  93        .set_mode       = lapic_timer_setup,
  94        .set_next_event = lapic_next_event,
  95        .broadcast      = lapic_timer_broadcast,
  96        .rating         = 100,
  97        .irq            = -1,
  98};
  99static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
 100
 101/* Local APIC was disabled by the BIOS and enabled by the kernel */
 102static int enabled_via_apicbase;
 103
 104/*
 105 * Get the LAPIC version
 106 */
 107static inline int lapic_get_version(void)
 108{
 109        return GET_APIC_VERSION(apic_read(APIC_LVR));
 110}
 111
 112/*
 113 * Check, if the APIC is integrated or a seperate chip
 114 */
 115static inline int lapic_is_integrated(void)
 116{
 117        return APIC_INTEGRATED(lapic_get_version());
 118}
 119
 120/*
 121 * Check, whether this is a modern or a first generation APIC
 122 */
 123static int modern_apic(void)
 124{
 125        /* AMD systems use old APIC versions, so check the CPU */
 126        if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
 127            boot_cpu_data.x86 >= 0xf)
 128                return 1;
 129        return lapic_get_version() >= 0x14;
 130}
 131
 132void apic_wait_icr_idle(void)
 133{
 134        while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
 135                cpu_relax();
 136}
 137
 138unsigned long safe_apic_wait_icr_idle(void)
 139{
 140        unsigned long send_status;
 141        int timeout;
 142
 143        timeout = 0;
 144        do {
 145                send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
 146                if (!send_status)
 147                        break;
 148                udelay(100);
 149        } while (timeout++ < 1000);
 150
 151        return send_status;
 152}
 153
 154/**
 155 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
 156 */
 157void enable_NMI_through_LVT0 (void * dummy)
 158{
 159        unsigned int v = APIC_DM_NMI;
 160
 161        /* Level triggered for 82489DX */
 162        if (!lapic_is_integrated())
 163                v |= APIC_LVT_LEVEL_TRIGGER;
 164        apic_write_around(APIC_LVT0, v);
 165}
 166
 167/**
 168 * get_physical_broadcast - Get number of physical broadcast IDs
 169 */
 170int get_physical_broadcast(void)
 171{
 172        return modern_apic() ? 0xff : 0xf;
 173}
 174
 175/**
 176 * lapic_get_maxlvt - get the maximum number of local vector table entries
 177 */
 178int lapic_get_maxlvt(void)
 179{
 180        unsigned int v = apic_read(APIC_LVR);
 181
 182        /* 82489DXs do not report # of LVT entries. */
 183        return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
 184}
 185
 186/*
 187 * Local APIC timer
 188 */
 189
 190/* Clock divisor is set to 16 */
 191#define APIC_DIVISOR 16
 192
 193/*
 194 * This function sets up the local APIC timer, with a timeout of
 195 * 'clocks' APIC bus clock. During calibration we actually call
 196 * this function twice on the boot CPU, once with a bogus timeout
 197 * value, second time for real. The other (noncalibrating) CPUs
 198 * call this function only once, with the real, calibrated value.
 199 *
 200 * We do reads before writes even if unnecessary, to get around the
 201 * P5 APIC double write bug.
 202 */
 203static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
 204{
 205        unsigned int lvtt_value, tmp_value;
 206
 207        lvtt_value = LOCAL_TIMER_VECTOR;
 208        if (!oneshot)
 209                lvtt_value |= APIC_LVT_TIMER_PERIODIC;
 210        if (!lapic_is_integrated())
 211                lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
 212
 213        if (!irqen)
 214                lvtt_value |= APIC_LVT_MASKED;
 215
 216        apic_write_around(APIC_LVTT, lvtt_value);
 217
 218        /*
 219         * Divide PICLK by 16
 220         */
 221        tmp_value = apic_read(APIC_TDCR);
 222        apic_write_around(APIC_TDCR, (tmp_value
 223                                & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
 224                                | APIC_TDR_DIV_16);
 225
 226        if (!oneshot)
 227                apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
 228}
 229
 230/*
 231 * Program the next event, relative to now
 232 */
 233static int lapic_next_event(unsigned long delta,
 234                            struct clock_event_device *evt)
 235{
 236        apic_write_around(APIC_TMICT, delta);
 237        return 0;
 238}
 239
 240/*
 241 * Setup the lapic timer in periodic or oneshot mode
 242 */
 243static void lapic_timer_setup(enum clock_event_mode mode,
 244                              struct clock_event_device *evt)
 245{
 246        unsigned long flags;
 247        unsigned int v;
 248
 249        /* Lapic used for broadcast ? */
 250        if (!local_apic_timer_verify_ok)
 251                return;
 252
 253        local_irq_save(flags);
 254
 255        switch (mode) {
 256        case CLOCK_EVT_MODE_PERIODIC:
 257        case CLOCK_EVT_MODE_ONESHOT:
 258                __setup_APIC_LVTT(calibration_result,
 259                                  mode != CLOCK_EVT_MODE_PERIODIC, 1);
 260                break;
 261        case CLOCK_EVT_MODE_UNUSED:
 262        case CLOCK_EVT_MODE_SHUTDOWN:
 263                v = apic_read(APIC_LVTT);
 264                v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
 265                apic_write_around(APIC_LVTT, v);
 266                break;
 267        }
 268
 269        local_irq_restore(flags);
 270}
 271
 272/*
 273 * Local APIC timer broadcast function
 274 */
 275static void lapic_timer_broadcast(cpumask_t mask)
 276{
 277#ifdef CONFIG_SMP
 278        send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
 279#endif
 280}
 281
 282/*
 283 * Setup the local APIC timer for this CPU. Copy the initilized values
 284 * of the boot CPU and register the clock event in the framework.
 285 */
 286static void __devinit setup_APIC_timer(void)
 287{
 288        struct clock_event_device *levt = &__get_cpu_var(lapic_events);
 289
 290        memcpy(levt, &lapic_clockevent, sizeof(*levt));
 291        levt->cpumask = cpumask_of_cpu(smp_processor_id());
 292
 293        clockevents_register_device(levt);
 294}
 295
 296/*
 297 * In this functions we calibrate APIC bus clocks to the external timer.
 298 *
 299 * We want to do the calibration only once since we want to have local timer
 300 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
 301 * frequency.
 302 *
 303 * This was previously done by reading the PIT/HPET and waiting for a wrap
 304 * around to find out, that a tick has elapsed. I have a box, where the PIT
 305 * readout is broken, so it never gets out of the wait loop again. This was
 306 * also reported by others.
 307 *
 308 * Monitoring the jiffies value is inaccurate and the clockevents
 309 * infrastructure allows us to do a simple substitution of the interrupt
 310 * handler.
 311 *
 312 * The calibration routine also uses the pm_timer when possible, as the PIT
 313 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
 314 * back to normal later in the boot process).
 315 */
 316
 317#define LAPIC_CAL_LOOPS         (HZ/10)
 318
 319static __initdata volatile int lapic_cal_loops = -1;
 320static __initdata long lapic_cal_t1, lapic_cal_t2;
 321static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
 322static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
 323static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
 324
 325/*
 326 * Temporary interrupt handler.
 327 */
 328static void __init lapic_cal_handler(struct clock_event_device *dev)
 329{
 330        unsigned long long tsc = 0;
 331        long tapic = apic_read(APIC_TMCCT);
 332        unsigned long pm = acpi_pm_read_early();
 333
 334        if (cpu_has_tsc)
 335                rdtscll(tsc);
 336
 337        switch (lapic_cal_loops++) {
 338        case 0:
 339                lapic_cal_t1 = tapic;
 340                lapic_cal_tsc1 = tsc;
 341                lapic_cal_pm1 = pm;
 342                lapic_cal_j1 = jiffies;
 343                break;
 344
 345        case LAPIC_CAL_LOOPS:
 346                lapic_cal_t2 = tapic;
 347                lapic_cal_tsc2 = tsc;
 348                if (pm < lapic_cal_pm1)
 349                        pm += ACPI_PM_OVRRUN;
 350                lapic_cal_pm2 = pm;
 351                lapic_cal_j2 = jiffies;
 352                break;
 353        }
 354}
 355
 356/*
 357 * Setup the boot APIC
 358 *
 359 * Calibrate and verify the result.
 360 */
 361void __init setup_boot_APIC_clock(void)
 362{
 363        struct clock_event_device *levt = &__get_cpu_var(lapic_events);
 364        const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
 365        const long pm_thresh = pm_100ms/100;
 366        void (*real_handler)(struct clock_event_device *dev);
 367        unsigned long deltaj;
 368        long delta, deltapm;
 369        int pm_referenced = 0;
 370
 371        /*
 372         * The local apic timer can be disabled via the kernel
 373         * commandline or from the CPU detection code. Register the lapic
 374         * timer as a dummy clock event source on SMP systems, so the
 375         * broadcast mechanism is used. On UP systems simply ignore it.
 376         */
 377        if (local_apic_timer_disabled) {
 378                /* No broadcast on UP ! */
 379                if (num_possible_cpus() > 1)
 380                        setup_APIC_timer();
 381                return;
 382        }
 383
 384        apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
 385                    "calibrating APIC timer ...\n");
 386
 387        local_irq_disable();
 388
 389        /* Replace the global interrupt handler */
 390        real_handler = global_clock_event->event_handler;
 391        global_clock_event->event_handler = lapic_cal_handler;
 392
 393        /*
 394         * Setup the APIC counter to 1e9. There is no way the lapic
 395         * can underflow in the 100ms detection time frame
 396         */
 397        __setup_APIC_LVTT(1000000000, 0, 0);
 398
 399        /* Let the interrupts run */
 400        local_irq_enable();
 401
 402        while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
 403                cpu_relax();
 404
 405        local_irq_disable();
 406
 407        /* Restore the real event handler */
 408        global_clock_event->event_handler = real_handler;
 409
 410        /* Build delta t1-t2 as apic timer counts down */
 411        delta = lapic_cal_t1 - lapic_cal_t2;
 412        apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
 413
 414        /* Check, if the PM timer is available */
 415        deltapm = lapic_cal_pm2 - lapic_cal_pm1;
 416        apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
 417
 418        if (deltapm) {
 419                unsigned long mult;
 420                u64 res;
 421
 422                mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
 423
 424                if (deltapm > (pm_100ms - pm_thresh) &&
 425                    deltapm < (pm_100ms + pm_thresh)) {
 426                        apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
 427                } else {
 428                        res = (((u64) deltapm) *  mult) >> 22;
 429                        do_div(res, 1000000);
 430                        printk(KERN_WARNING "APIC calibration not consistent "
 431                               "with PM Timer: %ldms instead of 100ms\n",
 432                               (long)res);
 433                        /* Correct the lapic counter value */
 434                        res = (((u64) delta ) * pm_100ms);
 435                        do_div(res, deltapm);
 436                        printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
 437                               "%lu (%ld)\n", (unsigned long) res, delta);
 438                        delta = (long) res;
 439                }
 440                pm_referenced = 1;
 441        }
 442
 443        /* Calculate the scaled math multiplication factor */
 444        lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS, 32);
 445        lapic_clockevent.max_delta_ns =
 446                clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
 447        lapic_clockevent.min_delta_ns =
 448                clockevent_delta2ns(0xF, &lapic_clockevent);
 449
 450        calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
 451
 452        apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
 453        apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
 454        apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
 455                    calibration_result);
 456
 457        if (cpu_has_tsc) {
 458                delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
 459                apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
 460                            "%ld.%04ld MHz.\n",
 461                            (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
 462                            (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
 463        }
 464
 465        apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
 466                    "%u.%04u MHz.\n",
 467                    calibration_result / (1000000 / HZ),
 468                    calibration_result % (1000000 / HZ));
 469
 470        local_apic_timer_verify_ok = 1;
 471
 472        /* We trust the pm timer based calibration */
 473        if (!pm_referenced) {
 474                apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
 475
 476                /*
 477                 * Setup the apic timer manually
 478                 */
 479                levt->event_handler = lapic_cal_handler;
 480                lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
 481                lapic_cal_loops = -1;
 482
 483                /* Let the interrupts run */
 484                local_irq_enable();
 485
 486                while(lapic_cal_loops <= LAPIC_CAL_LOOPS)
 487                        cpu_relax();
 488
 489                local_irq_disable();
 490
 491                /* Stop the lapic timer */
 492                lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
 493
 494                local_irq_enable();
 495
 496                /* Jiffies delta */
 497                deltaj = lapic_cal_j2 - lapic_cal_j1;
 498                apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
 499
 500                /* Check, if the jiffies result is consistent */
 501                if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
 502                        apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
 503                else
 504                        local_apic_timer_verify_ok = 0;
 505        } else
 506                local_irq_enable();
 507
 508        if (!local_apic_timer_verify_ok) {
 509                printk(KERN_WARNING
 510                       "APIC timer disabled due to verification failure.\n");
 511                /* No broadcast on UP ! */
 512                if (num_possible_cpus() == 1)
 513                        return;
 514        } else {
 515                /*
 516                 * If nmi_watchdog is set to IO_APIC, we need the
 517                 * PIT/HPET going.  Otherwise register lapic as a dummy
 518                 * device.
 519                 */
 520                if (nmi_watchdog != NMI_IO_APIC)
 521                        lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
 522        }
 523
 524        /* Setup the lapic or request the broadcast */
 525        setup_APIC_timer();
 526}
 527
 528void __devinit setup_secondary_APIC_clock(void)
 529{
 530        setup_APIC_timer();
 531}
 532
 533/*
 534 * The guts of the apic timer interrupt
 535 */
 536static void local_apic_timer_interrupt(void)
 537{
 538        int cpu = smp_processor_id();
 539        struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
 540
 541        /*
 542         * Normally we should not be here till LAPIC has been initialized but
 543         * in some cases like kdump, its possible that there is a pending LAPIC
 544         * timer interrupt from previous kernel's context and is delivered in
 545         * new kernel the moment interrupts are enabled.
 546         *
 547         * Interrupts are enabled early and LAPIC is setup much later, hence
 548         * its possible that when we get here evt->event_handler is NULL.
 549         * Check for event_handler being NULL and discard the interrupt as
 550         * spurious.
 551         */
 552        if (!evt->event_handler) {
 553                printk(KERN_WARNING
 554                       "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
 555                /* Switch it off */
 556                lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
 557                return;
 558        }
 559
 560        per_cpu(irq_stat, cpu).apic_timer_irqs++;
 561
 562        evt->event_handler(evt);
 563}
 564
 565/*
 566 * Local APIC timer interrupt. This is the most natural way for doing
 567 * local interrupts, but local timer interrupts can be emulated by
 568 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
 569 *
 570 * [ if a single-CPU system runs an SMP kernel then we call the local
 571 *   interrupt as well. Thus we cannot inline the local irq ... ]
 572 */
 573
 574void fastcall smp_apic_timer_interrupt(struct pt_regs *regs)
 575{
 576        struct pt_regs *old_regs = set_irq_regs(regs);
 577
 578        /*
 579         * NOTE! We'd better ACK the irq immediately,
 580         * because timer handling can be slow.
 581         */
 582        ack_APIC_irq();
 583        /*
 584         * update_process_times() expects us to have done irq_enter().
 585         * Besides, if we don't timer interrupts ignore the global
 586         * interrupt lock, which is the WrongThing (tm) to do.
 587         */
 588        irq_enter();
 589        local_apic_timer_interrupt();
 590        irq_exit();
 591
 592        set_irq_regs(old_regs);
 593}
 594
 595int setup_profiling_timer(unsigned int multiplier)
 596{
 597        return -EINVAL;
 598}
 599
 600/*
 601 * Local APIC start and shutdown
 602 */
 603
 604/**
 605 * clear_local_APIC - shutdown the local APIC
 606 *
 607 * This is called, when a CPU is disabled and before rebooting, so the state of
 608 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
 609 * leftovers during boot.
 610 */
 611void clear_local_APIC(void)
 612{
 613        int maxlvt = lapic_get_maxlvt();
 614        unsigned long v;
 615
 616        /*
 617         * Masking an LVT entry can trigger a local APIC error
 618         * if the vector is zero. Mask LVTERR first to prevent this.
 619         */
 620        if (maxlvt >= 3) {
 621                v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
 622                apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
 623        }
 624        /*
 625         * Careful: we have to set masks only first to deassert
 626         * any level-triggered sources.
 627         */
 628        v = apic_read(APIC_LVTT);
 629        apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
 630        v = apic_read(APIC_LVT0);
 631        apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
 632        v = apic_read(APIC_LVT1);
 633        apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
 634        if (maxlvt >= 4) {
 635                v = apic_read(APIC_LVTPC);
 636                apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
 637        }
 638
 639        /* lets not touch this if we didn't frob it */
 640#ifdef CONFIG_X86_MCE_P4THERMAL
 641        if (maxlvt >= 5) {
 642                v = apic_read(APIC_LVTTHMR);
 643                apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
 644        }
 645#endif
 646        /*
 647         * Clean APIC state for other OSs:
 648         */
 649        apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
 650        apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
 651        apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
 652        if (maxlvt >= 3)
 653                apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
 654        if (maxlvt >= 4)
 655                apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
 656
 657#ifdef CONFIG_X86_MCE_P4THERMAL
 658        if (maxlvt >= 5)
 659                apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
 660#endif
 661        /* Integrated APIC (!82489DX) ? */
 662        if (lapic_is_integrated()) {
 663                if (maxlvt > 3)
 664                        /* Clear ESR due to Pentium errata 3AP and 11AP */
 665                        apic_write(APIC_ESR, 0);
 666                apic_read(APIC_ESR);
 667        }
 668}
 669
 670/**
 671 * disable_local_APIC - clear and disable the local APIC
 672 */
 673void disable_local_APIC(void)
 674{
 675        unsigned long value;
 676
 677        clear_local_APIC();
 678
 679        /*
 680         * Disable APIC (implies clearing of registers
 681         * for 82489DX!).
 682         */
 683        value = apic_read(APIC_SPIV);
 684        value &= ~APIC_SPIV_APIC_ENABLED;
 685        apic_write_around(APIC_SPIV, value);
 686
 687        /*
 688         * When LAPIC was disabled by the BIOS and enabled by the kernel,
 689         * restore the disabled state.
 690         */
 691        if (enabled_via_apicbase) {
 692                unsigned int l, h;
 693
 694                rdmsr(MSR_IA32_APICBASE, l, h);
 695                l &= ~MSR_IA32_APICBASE_ENABLE;
 696                wrmsr(MSR_IA32_APICBASE, l, h);
 697        }
 698}
 699
 700/*
 701 * If Linux enabled the LAPIC against the BIOS default disable it down before
 702 * re-entering the BIOS on shutdown.  Otherwise the BIOS may get confused and
 703 * not power-off.  Additionally clear all LVT entries before disable_local_APIC
 704 * for the case where Linux didn't enable the LAPIC.
 705 */
 706void lapic_shutdown(void)
 707{
 708        unsigned long flags;
 709
 710        if (!cpu_has_apic)
 711                return;
 712
 713        local_irq_save(flags);
 714        clear_local_APIC();
 715
 716        if (enabled_via_apicbase)
 717                disable_local_APIC();
 718
 719        local_irq_restore(flags);
 720}
 721
 722/*
 723 * This is to verify that we're looking at a real local APIC.
 724 * Check these against your board if the CPUs aren't getting
 725 * started for no apparent reason.
 726 */
 727int __init verify_local_APIC(void)
 728{
 729        unsigned int reg0, reg1;
 730
 731        /*
 732         * The version register is read-only in a real APIC.
 733         */
 734        reg0 = apic_read(APIC_LVR);
 735        apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
 736        apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
 737        reg1 = apic_read(APIC_LVR);
 738        apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
 739
 740        /*
 741         * The two version reads above should print the same
 742         * numbers.  If the second one is different, then we
 743         * poke at a non-APIC.
 744         */
 745        if (reg1 != reg0)
 746                return 0;
 747
 748        /*
 749         * Check if the version looks reasonably.
 750         */
 751        reg1 = GET_APIC_VERSION(reg0);
 752        if (reg1 == 0x00 || reg1 == 0xff)
 753                return 0;
 754        reg1 = lapic_get_maxlvt();
 755        if (reg1 < 0x02 || reg1 == 0xff)
 756                return 0;
 757
 758        /*
 759         * The ID register is read/write in a real APIC.
 760         */
 761        reg0 = apic_read(APIC_ID);
 762        apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
 763
 764        /*
 765         * The next two are just to see if we have sane values.
 766         * They're only really relevant if we're in Virtual Wire
 767         * compatibility mode, but most boxes are anymore.
 768         */
 769        reg0 = apic_read(APIC_LVT0);
 770        apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
 771        reg1 = apic_read(APIC_LVT1);
 772        apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
 773
 774        return 1;
 775}
 776
 777/**
 778 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
 779 */
 780void __init sync_Arb_IDs(void)
 781{
 782        /*
 783         * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
 784         * needed on AMD.
 785         */
 786        if (modern_apic())
 787                return;
 788        /*
 789         * Wait for idle.
 790         */
 791        apic_wait_icr_idle();
 792
 793        apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
 794        apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
 795                                | APIC_DM_INIT);
 796}
 797
 798/*
 799 * An initial setup of the virtual wire mode.
 800 */
 801void __init init_bsp_APIC(void)
 802{
 803        unsigned long value;
 804
 805        /*
 806         * Don't do the setup now if we have a SMP BIOS as the
 807         * through-I/O-APIC virtual wire mode might be active.
 808         */
 809        if (smp_found_config || !cpu_has_apic)
 810                return;
 811
 812        /*
 813         * Do not trust the local APIC being empty at bootup.
 814         */
 815        clear_local_APIC();
 816
 817        /*
 818         * Enable APIC.
 819         */
 820        value = apic_read(APIC_SPIV);
 821        value &= ~APIC_VECTOR_MASK;
 822        value |= APIC_SPIV_APIC_ENABLED;
 823
 824        /* This bit is reserved on P4/Xeon and should be cleared */
 825        if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
 826            (boot_cpu_data.x86 == 15))
 827                value &= ~APIC_SPIV_FOCUS_DISABLED;
 828        else
 829                value |= APIC_SPIV_FOCUS_DISABLED;
 830        value |= SPURIOUS_APIC_VECTOR;
 831        apic_write_around(APIC_SPIV, value);
 832
 833        /*
 834         * Set up the virtual wire mode.
 835         */
 836        apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
 837        value = APIC_DM_NMI;
 838        if (!lapic_is_integrated())             /* 82489DX */
 839                value |= APIC_LVT_LEVEL_TRIGGER;
 840        apic_write_around(APIC_LVT1, value);
 841}
 842
 843/**
 844 * setup_local_APIC - setup the local APIC
 845 */
 846void __devinit setup_local_APIC(void)
 847{
 848        unsigned long oldvalue, value, maxlvt, integrated;
 849        int i, j;
 850
 851        /* Pound the ESR really hard over the head with a big hammer - mbligh */
 852        if (esr_disable) {
 853                apic_write(APIC_ESR, 0);
 854                apic_write(APIC_ESR, 0);
 855                apic_write(APIC_ESR, 0);
 856                apic_write(APIC_ESR, 0);
 857        }
 858
 859        integrated = lapic_is_integrated();
 860
 861        /*
 862         * Double-check whether this APIC is really registered.
 863         */
 864        if (!apic_id_registered())
 865                BUG();
 866
 867        /*
 868         * Intel recommends to set DFR, LDR and TPR before enabling
 869         * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel
 870         * document number 292116).  So here it goes...
 871         */
 872        init_apic_ldr();
 873
 874        /*
 875         * Set Task Priority to 'accept all'. We never change this
 876         * later on.
 877         */
 878        value = apic_read(APIC_TASKPRI);
 879        value &= ~APIC_TPRI_MASK;
 880        apic_write_around(APIC_TASKPRI, value);
 881
 882        /*
 883         * After a crash, we no longer service the interrupts and a pending
 884         * interrupt from previous kernel might still have ISR bit set.
 885         *
 886         * Most probably by now CPU has serviced that pending interrupt and
 887         * it might not have done the ack_APIC_irq() because it thought,
 888         * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
 889         * does not clear the ISR bit and cpu thinks it has already serivced
 890         * the interrupt. Hence a vector might get locked. It was noticed
 891         * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
 892         */
 893        for (i = APIC_ISR_NR - 1; i >= 0; i--) {
 894                value = apic_read(APIC_ISR + i*0x10);
 895                for (j = 31; j >= 0; j--) {
 896                        if (value & (1<<j))
 897                                ack_APIC_irq();
 898                }
 899        }
 900
 901        /*
 902         * Now that we are all set up, enable the APIC
 903         */
 904        value = apic_read(APIC_SPIV);
 905        value &= ~APIC_VECTOR_MASK;
 906        /*
 907         * Enable APIC
 908         */
 909        value |= APIC_SPIV_APIC_ENABLED;
 910
 911        /*
 912         * Some unknown Intel IO/APIC (or APIC) errata is biting us with
 913         * certain networking cards. If high frequency interrupts are
 914         * happening on a particular IOAPIC pin, plus the IOAPIC routing
 915         * entry is masked/unmasked at a high rate as well then sooner or
 916         * later IOAPIC line gets 'stuck', no more interrupts are received
 917         * from the device. If focus CPU is disabled then the hang goes
 918         * away, oh well :-(
 919         *
 920         * [ This bug can be reproduced easily with a level-triggered
 921         *   PCI Ne2000 networking cards and PII/PIII processors, dual
 922         *   BX chipset. ]
 923         */
 924        /*
 925         * Actually disabling the focus CPU check just makes the hang less
 926         * frequent as it makes the interrupt distributon model be more
 927         * like LRU than MRU (the short-term load is more even across CPUs).
 928         * See also the comment in end_level_ioapic_irq().  --macro
 929         */
 930
 931        /* Enable focus processor (bit==0) */
 932        value &= ~APIC_SPIV_FOCUS_DISABLED;
 933
 934        /*
 935         * Set spurious IRQ vector
 936         */
 937        value |= SPURIOUS_APIC_VECTOR;
 938        apic_write_around(APIC_SPIV, value);
 939
 940        /*
 941         * Set up LVT0, LVT1:
 942         *
 943         * set up through-local-APIC on the BP's LINT0. This is not
 944         * strictly necessery in pure symmetric-IO mode, but sometimes
 945         * we delegate interrupts to the 8259A.
 946         */
 947        /*
 948         * TODO: set up through-local-APIC from through-I/O-APIC? --macro
 949         */
 950        value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
 951        if (!smp_processor_id() && (pic_mode || !value)) {
 952                value = APIC_DM_EXTINT;
 953                apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
 954                                smp_processor_id());
 955        } else {
 956                value = APIC_DM_EXTINT | APIC_LVT_MASKED;
 957                apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
 958                                smp_processor_id());
 959        }
 960        apic_write_around(APIC_LVT0, value);
 961
 962        /*
 963         * only the BP should see the LINT1 NMI signal, obviously.
 964         */
 965        if (!smp_processor_id())
 966                value = APIC_DM_NMI;
 967        else
 968                value = APIC_DM_NMI | APIC_LVT_MASKED;
 969        if (!integrated)                /* 82489DX */
 970                value |= APIC_LVT_LEVEL_TRIGGER;
 971        apic_write_around(APIC_LVT1, value);
 972
 973        if (integrated && !esr_disable) {               /* !82489DX */
 974                maxlvt = lapic_get_maxlvt();
 975                if (maxlvt > 3)         /* Due to the Pentium erratum 3AP. */
 976                        apic_write(APIC_ESR, 0);
 977                oldvalue = apic_read(APIC_ESR);
 978
 979                /* enables sending errors */
 980                value = ERROR_APIC_VECTOR;
 981                apic_write_around(APIC_LVTERR, value);
 982                /*
 983                 * spec says clear errors after enabling vector.
 984                 */
 985                if (maxlvt > 3)
 986                        apic_write(APIC_ESR, 0);
 987                value = apic_read(APIC_ESR);
 988                if (value != oldvalue)
 989                        apic_printk(APIC_VERBOSE, "ESR value before enabling "
 990                                "vector: 0x%08lx  after: 0x%08lx\n",
 991                                oldvalue, value);
 992        } else {
 993                if (esr_disable)
 994                        /*
 995                         * Something untraceble is creating bad interrupts on
 996                         * secondary quads ... for the moment, just leave the
 997                         * ESR disabled - we can't do anything useful with the
 998                         * errors anyway - mbligh
 999                         */
1000                        printk(KERN_INFO "Leaving ESR disabled.\n");
1001                else
1002                        printk(KERN_INFO "No ESR for 82489DX.\n");
1003        }
1004
1005        /* Disable the local apic timer */
1006        value = apic_read(APIC_LVTT);
1007        value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1008        apic_write_around(APIC_LVTT, value);
1009
1010        setup_apic_nmi_watchdog(NULL);
1011        apic_pm_activate();
1012}
1013
1014/*
1015 * Detect and initialize APIC
1016 */
1017static int __init detect_init_APIC (void)
1018{
1019        u32 h, l, features;
1020
1021        /* Disabled by kernel option? */
1022        if (enable_local_apic < 0)
1023                return -1;
1024
1025        switch (boot_cpu_data.x86_vendor) {
1026        case X86_VENDOR_AMD:
1027                if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1028                    (boot_cpu_data.x86 == 15))
1029                        break;
1030                goto no_apic;
1031        case X86_VENDOR_INTEL:
1032                if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1033                    (boot_cpu_data.x86 == 5 && cpu_has_apic))
1034                        break;
1035                goto no_apic;
1036        default:
1037                goto no_apic;
1038        }
1039
1040        if (!cpu_has_apic) {
1041                /*
1042                 * Over-ride BIOS and try to enable the local APIC only if
1043                 * "lapic" specified.
1044                 */
1045                if (enable_local_apic <= 0) {
1046                        printk(KERN_INFO "Local APIC disabled by BIOS -- "
1047                               "you can enable it with \"lapic\"\n");
1048                        return -1;
1049                }
1050                /*
1051                 * Some BIOSes disable the local APIC in the APIC_BASE
1052                 * MSR. This can only be done in software for Intel P6 or later
1053                 * and AMD K7 (Model > 1) or later.
1054                 */
1055                rdmsr(MSR_IA32_APICBASE, l, h);
1056                if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1057                        printk(KERN_INFO
1058                               "Local APIC disabled by BIOS -- reenabling.\n");
1059                        l &= ~MSR_IA32_APICBASE_BASE;
1060                        l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1061                        wrmsr(MSR_IA32_APICBASE, l, h);
1062                        enabled_via_apicbase = 1;
1063                }
1064        }
1065        /*
1066         * The APIC feature bit should now be enabled
1067         * in `cpuid'
1068         */
1069        features = cpuid_edx(1);
1070        if (!(features & (1 << X86_FEATURE_APIC))) {
1071                printk(KERN_WARNING "Could not enable APIC!\n");
1072                return -1;
1073        }
1074        set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1075        mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1076
1077        /* The BIOS may have set up the APIC at some other address */
1078        rdmsr(MSR_IA32_APICBASE, l, h);
1079        if (l & MSR_IA32_APICBASE_ENABLE)
1080                mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1081
1082        if (nmi_watchdog != NMI_NONE)
1083                nmi_watchdog = NMI_LOCAL_APIC;
1084
1085        printk(KERN_INFO "Found and enabled local APIC!\n");
1086
1087        apic_pm_activate();
1088
1089        return 0;
1090
1091no_apic:
1092        printk(KERN_INFO "No local APIC present or hardware disabled\n");
1093        return -1;
1094}
1095
1096/**
1097 * init_apic_mappings - initialize APIC mappings
1098 */
1099void __init init_apic_mappings(void)
1100{
1101        unsigned long apic_phys;
1102
1103        /*
1104         * If no local APIC can be found then set up a fake all
1105         * zeroes page to simulate the local APIC and another
1106         * one for the IO-APIC.
1107         */
1108        if (!smp_found_config && detect_init_APIC()) {
1109                apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1110                apic_phys = __pa(apic_phys);
1111        } else
1112                apic_phys = mp_lapic_addr;
1113
1114        set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1115        printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1116               apic_phys);
1117
1118        /*
1119         * Fetch the APIC ID of the BSP in case we have a
1120         * default configuration (or the MP table is broken).
1121         */
1122        if (boot_cpu_physical_apicid == -1U)
1123                boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1124
1125#ifdef CONFIG_X86_IO_APIC
1126        {
1127                unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1128                int i;
1129
1130                for (i = 0; i < nr_ioapics; i++) {
1131                        if (smp_found_config) {
1132                                ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1133                                if (!ioapic_phys) {
1134                                        printk(KERN_ERR
1135                                               "WARNING: bogus zero IO-APIC "
1136                                               "address found in MPTABLE, "
1137                                               "disabling IO/APIC support!\n");
1138                                        smp_found_config = 0;
1139                                        skip_ioapic_setup = 1;
1140                                        goto fake_ioapic_page;
1141                                }
1142                        } else {
1143fake_ioapic_page:
1144                                ioapic_phys = (unsigned long)
1145                                              alloc_bootmem_pages(PAGE_SIZE);
1146                                ioapic_phys = __pa(ioapic_phys);
1147                        }
1148                        set_fixmap_nocache(idx, ioapic_phys);
1149                        printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1150                               __fix_to_virt(idx), ioapic_phys);
1151                        idx++;
1152                }
1153        }
1154#endif
1155}
1156
1157/*
1158 * This initializes the IO-APIC and APIC hardware if this is
1159 * a UP kernel.
1160 */
1161int __init APIC_init_uniprocessor (void)
1162{
1163        if (enable_local_apic < 0)
1164                clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1165
1166        if (!smp_found_config && !cpu_has_apic)
1167                return -1;
1168
1169        /*
1170         * Complain if the BIOS pretends there is one.
1171         */
1172        if (!cpu_has_apic &&
1173            APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1174                printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1175                       boot_cpu_physical_apicid);
1176                clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1177                return -1;
1178        }
1179
1180        verify_local_APIC();
1181
1182        connect_bsp_APIC();
1183
1184        /*
1185         * Hack: In case of kdump, after a crash, kernel might be booting
1186         * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1187         * might be zero if read from MP tables. Get it from LAPIC.
1188         */
1189#ifdef CONFIG_CRASH_DUMP
1190        boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1191#endif
1192        phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1193
1194        setup_local_APIC();
1195
1196#ifdef CONFIG_X86_IO_APIC
1197        if (smp_found_config)
1198                if (!skip_ioapic_setup && nr_ioapics)
1199                        setup_IO_APIC();
1200#endif
1201        setup_boot_clock();
1202
1203        return 0;
1204}
1205
1206/*
1207 * APIC command line parameters
1208 */
1209static int __init parse_lapic(char *arg)
1210{
1211        enable_local_apic = 1;
1212        return 0;
1213}
1214early_param("lapic", parse_lapic);
1215
1216static int __init parse_nolapic(char *arg)
1217{
1218        enable_local_apic = -1;
1219        clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1220        return 0;
1221}
1222early_param("nolapic", parse_nolapic);
1223
1224static int __init parse_disable_lapic_timer(char *arg)
1225{
1226        local_apic_timer_disabled = 1;
1227        return 0;
1228}
1229early_param("nolapic_timer", parse_disable_lapic_timer);
1230
1231static int __init parse_lapic_timer_c2_ok(char *arg)
1232{
1233        local_apic_timer_c2_ok = 1;
1234        return 0;
1235}
1236early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1237
1238static int __init apic_set_verbosity(char *str)
1239{
1240        if (strcmp("debug", str) == 0)
1241                apic_verbosity = APIC_DEBUG;
1242        else if (strcmp("verbose", str) == 0)
1243                apic_verbosity = APIC_VERBOSE;
1244        return 1;
1245}
1246
1247__setup("apic=", apic_set_verbosity);
1248
1249
1250/*
1251 * Local APIC interrupts
1252 */
1253
1254/*
1255 * This interrupt should _never_ happen with our APIC/SMP architecture
1256 */
1257void smp_spurious_interrupt(struct pt_regs *regs)
1258{
1259        unsigned long v;
1260
1261        irq_enter();
1262        /*
1263         * Check if this really is a spurious interrupt and ACK it
1264         * if it is a vectored one.  Just in case...
1265         * Spurious interrupts should not be ACKed.
1266         */
1267        v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1268        if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1269                ack_APIC_irq();
1270
1271        /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1272        printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1273               "should never happen.\n", smp_processor_id());
1274        irq_exit();
1275}
1276
1277/*
1278 * This interrupt should never happen with our APIC/SMP architecture
1279 */
1280void smp_error_interrupt(struct pt_regs *regs)
1281{
1282        unsigned long v, v1;
1283
1284        irq_enter();
1285        /* First tickle the hardware, only then report what went on. -- REW */
1286        v = apic_read(APIC_ESR);
1287        apic_write(APIC_ESR, 0);
1288        v1 = apic_read(APIC_ESR);
1289        ack_APIC_irq();
1290        atomic_inc(&irq_err_count);
1291
1292        /* Here is what the APIC error bits mean:
1293           0: Send CS error
1294           1: Receive CS error
1295           2: Send accept error
1296           3: Receive accept error
1297           4: Reserved
1298           5: Send illegal vector
1299           6: Received illegal vector
1300           7: Illegal register address
1301        */
1302        printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1303                smp_processor_id(), v , v1);
1304        irq_exit();
1305}
1306
1307/*
1308 * Initialize APIC interrupts
1309 */
1310void __init apic_intr_init(void)
1311{
1312#ifdef CONFIG_SMP
1313        smp_intr_init();
1314#endif
1315        /* self generated IPI for local APIC timer */
1316        set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1317
1318        /* IPI vectors for APIC spurious and error interrupts */
1319        set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1320        set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1321
1322        /* thermal monitor LVT interrupt */
1323#ifdef CONFIG_X86_MCE_P4THERMAL
1324        set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1325#endif
1326}
1327
1328/**
1329 * connect_bsp_APIC - attach the APIC to the interrupt system
1330 */
1331void __init connect_bsp_APIC(void)
1332{
1333        if (pic_mode) {
1334                /*
1335                 * Do not trust the local APIC being empty at bootup.
1336                 */
1337                clear_local_APIC();
1338                /*
1339                 * PIC mode, enable APIC mode in the IMCR, i.e.  connect BSP's
1340                 * local APIC to INT and NMI lines.
1341                 */
1342                apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1343                                "enabling APIC mode.\n");
1344                outb(0x70, 0x22);
1345                outb(0x01, 0x23);
1346        }
1347        enable_apic_mode();
1348}
1349
1350/**
1351 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1352 * @virt_wire_setup:    indicates, whether virtual wire mode is selected
1353 *
1354 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1355 * APIC is disabled.
1356 */
1357void disconnect_bsp_APIC(int virt_wire_setup)
1358{
1359        if (pic_mode) {
1360                /*
1361                 * Put the board back into PIC mode (has an effect only on
1362                 * certain older boards).  Note that APIC interrupts, including
1363                 * IPIs, won't work beyond this point!  The only exception are
1364                 * INIT IPIs.
1365                 */
1366                apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1367                                "entering PIC mode.\n");
1368                outb(0x70, 0x22);
1369                outb(0x00, 0x23);
1370        } else {
1371                /* Go back to Virtual Wire compatibility mode */
1372                unsigned long value;
1373
1374                /* For the spurious interrupt use vector F, and enable it */
1375                value = apic_read(APIC_SPIV);
1376                value &= ~APIC_VECTOR_MASK;
1377                value |= APIC_SPIV_APIC_ENABLED;
1378                value |= 0xf;
1379                apic_write_around(APIC_SPIV, value);
1380
1381                if (!virt_wire_setup) {
1382                        /*
1383                         * For LVT0 make it edge triggered, active high,
1384                         * external and enabled
1385                         */
1386                        value = apic_read(APIC_LVT0);
1387                        value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1388                                APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1389                                APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
1390                        value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1391                        value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1392                        apic_write_around(APIC_LVT0, value);
1393                } else {
1394                        /* Disable LVT0 */
1395                        apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1396                }
1397
1398                /*
1399                 * For LVT1 make it edge triggered, active high, nmi and
1400                 * enabled
1401                 */
1402                value = apic_read(APIC_LVT1);
1403                value &= ~(
1404                        APIC_MODE_MASK | APIC_SEND_PENDING |
1405                        APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1406                        APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1407                value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1408                value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1409                apic_write_around(APIC_LVT1, value);
1410        }
1411}
1412
1413/*
1414 * Power management
1415 */
1416#ifdef CONFIG_PM
1417
1418static struct {
1419        int active;
1420        /* r/w apic fields */
1421        unsigned int apic_id;
1422        unsigned int apic_taskpri;
1423        unsigned int apic_ldr;
1424        unsigned int apic_dfr;
1425        unsigned int apic_spiv;
1426        unsigned int apic_lvtt;
1427        unsigned int apic_lvtpc;
1428        unsigned int apic_lvt0;
1429        unsigned int apic_lvt1;
1430        unsigned int apic_lvterr;
1431        unsigned int apic_tmict;
1432        unsigned int apic_tdcr;
1433        unsigned int apic_thmr;
1434} apic_pm_state;
1435
1436static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1437{
1438        unsigned long flags;
1439        int maxlvt;
1440
1441        if (!apic_pm_state.active)
1442                return 0;
1443
1444        maxlvt = lapic_get_maxlvt();
1445
1446        apic_pm_state.apic_id = apic_read(APIC_ID);
1447        apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1448        apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1449        apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1450        apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1451        apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1452        if (maxlvt >= 4)
1453                apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1454        apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1455        apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1456        apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1457        apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1458        apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1459#ifdef CONFIG_X86_MCE_P4THERMAL
1460        if (maxlvt >= 5)
1461                apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1462#endif
1463
1464        local_irq_save(flags);
1465        disable_local_APIC();
1466        local_irq_restore(flags);
1467        return 0;
1468}
1469
1470static int lapic_resume(struct sys_device *dev)
1471{
1472        unsigned int l, h;
1473        unsigned long flags;
1474        int maxlvt;
1475
1476        if (!apic_pm_state.active)
1477                return 0;
1478
1479        maxlvt = lapic_get_maxlvt();
1480
1481        local_irq_save(flags);
1482
1483        /*
1484         * Make sure the APICBASE points to the right address
1485         *
1486         * FIXME! This will be wrong if we ever support suspend on
1487         * SMP! We'll need to do this as part of the CPU restore!
1488         */
1489        rdmsr(MSR_IA32_APICBASE, l, h);
1490        l &= ~MSR_IA32_APICBASE_BASE;
1491        l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1492        wrmsr(MSR_IA32_APICBASE, l, h);
1493
1494        apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1495        apic_write(APIC_ID, apic_pm_state.apic_id);
1496        apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1497        apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1498        apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1499        apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1500        apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1501        apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1502#ifdef CONFIG_X86_MCE_P4THERMAL
1503        if (maxlvt >= 5)
1504                apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1505#endif
1506        if (maxlvt >= 4)
1507                apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1508        apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1509        apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1510        apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1511        apic_write(APIC_ESR, 0);
1512        apic_read(APIC_ESR);
1513        apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1514        apic_write(APIC_ESR, 0);
1515        apic_read(APIC_ESR);
1516        local_irq_restore(flags);
1517        return 0;
1518}
1519
1520/*
1521 * This device has no shutdown method - fully functioning local APICs
1522 * are needed on every CPU up until machine_halt/restart/poweroff.
1523 */
1524
1525static struct sysdev_class lapic_sysclass = {
1526        set_kset_name("lapic"),
1527        .resume         = lapic_resume,
1528        .suspend        = lapic_suspend,
1529};
1530
1531static struct sys_device device_lapic = {
1532        .id     = 0,
1533        .cls    = &lapic_sysclass,
1534};
1535
1536static void __devinit apic_pm_activate(void)
1537{
1538        apic_pm_state.active = 1;
1539}
1540
1541static int __init init_lapic_sysfs(void)
1542{
1543        int error;
1544
1545        if (!cpu_has_apic)
1546                return 0;
1547        /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1548
1549        error = sysdev_class_register(&lapic_sysclass);
1550        if (!error)
1551                error = sysdev_register(&device_lapic);
1552        return error;
1553}
1554device_initcall(init_lapic_sysfs);
1555
1556#else   /* CONFIG_PM */
1557
1558static void apic_pm_activate(void) { }
1559
1560#endif  /* CONFIG_PM */
1561
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.