linux-old/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 */
  14
  15#include <linux/config.h>
  16#include <linux/init.h>
  17
  18#include <linux/mm.h>
  19#include <linux/irq.h>
  20#include <linux/delay.h>
  21#include <linux/bootmem.h>
  22#include <linux/smp_lock.h>
  23#include <linux/interrupt.h>
  24#include <linux/mc146818rtc.h>
  25#include <linux/kernel_stat.h>
  26
  27#include <asm/atomic.h>
  28#include <asm/smp.h>
  29#include <asm/mtrr.h>
  30#include <asm/mpspec.h>
  31#include <asm/pgalloc.h>
  32#include <asm/smpboot.h>
  33
  34/* Using APIC to generate smp_local_timer_interrupt? */
  35int using_apic_timer = 0;
  36
  37int prof_multiplier[NR_CPUS] = { 1, };
  38int prof_old_multiplier[NR_CPUS] = { 1, };
  39int prof_counter[NR_CPUS] = { 1, };
  40
  41int get_maxlvt(void)
  42{
  43        unsigned int v, ver, maxlvt;
  44
  45        v = apic_read(APIC_LVR);
  46        ver = GET_APIC_VERSION(v);
  47        /* 82489DXs do not report # of LVT entries. */
  48        maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
  49        return maxlvt;
  50}
  51
  52void clear_local_APIC(void)
  53{
  54        int maxlvt;
  55        unsigned long v;
  56
  57        maxlvt = get_maxlvt();
  58
  59        /*
  60         * Masking an LVT entry on a P6 can trigger a local APIC error
  61         * if the vector is zero. Mask LVTERR first to prevent this.
  62         */
  63        if (maxlvt >= 3) {
  64                v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  65                apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
  66        }
  67        /*
  68         * Careful: we have to set masks only first to deassert
  69         * any level-triggered sources.
  70         */
  71        v = apic_read(APIC_LVTT);
  72        apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
  73        v = apic_read(APIC_LVT0);
  74        apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
  75        v = apic_read(APIC_LVT1);
  76        apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
  77        if (maxlvt >= 4) {
  78                v = apic_read(APIC_LVTPC);
  79                apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
  80        }
  81
  82        /*
  83         * Clean APIC state for other OSs:
  84         */
  85        apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
  86        apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
  87        apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
  88        if (maxlvt >= 3)
  89                apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
  90        if (maxlvt >= 4)
  91                apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
  92        v = GET_APIC_VERSION(apic_read(APIC_LVR));
  93        if (APIC_INTEGRATED(v)) {       /* !82489DX */
  94                if (maxlvt > 3)
  95                        apic_write(APIC_ESR, 0);
  96                apic_read(APIC_ESR);
  97        }
  98}
  99
 100void __init connect_bsp_APIC(void)
 101{
 102        if (pic_mode) {
 103                /*
 104                 * Do not trust the local APIC being empty at bootup.
 105                 */
 106                clear_local_APIC();
 107                /*
 108                 * PIC mode, enable APIC mode in the IMCR, i.e.
 109                 * connect BSP's local APIC to INT and NMI lines.
 110                 */
 111                printk("leaving PIC mode, enabling APIC mode.\n");
 112                outb(0x70, 0x22);
 113                outb(0x01, 0x23);
 114        }
 115}
 116
 117void disconnect_bsp_APIC(void)
 118{
 119        if (pic_mode) {
 120                /*
 121                 * Put the board back into PIC mode (has an effect
 122                 * only on certain older boards).  Note that APIC
 123                 * interrupts, including IPIs, won't work beyond
 124                 * this point!  The only exception are INIT IPIs.
 125                 */
 126                printk("disabling APIC mode, entering PIC mode.\n");
 127                outb(0x70, 0x22);
 128                outb(0x00, 0x23);
 129        }
 130}
 131
 132void disable_local_APIC(void)
 133{
 134        unsigned long value;
 135
 136        clear_local_APIC();
 137
 138        /*
 139         * Disable APIC (implies clearing of registers
 140         * for 82489DX!).
 141         */
 142        value = apic_read(APIC_SPIV);
 143        value &= ~APIC_SPIV_APIC_ENABLED;
 144        apic_write_around(APIC_SPIV, value);
 145}
 146
 147/*
 148 * This is to verify that we're looking at a real local APIC.
 149 * Check these against your board if the CPUs aren't getting
 150 * started for no apparent reason.
 151 */
 152int __init verify_local_APIC(void)
 153{
 154        unsigned int reg0, reg1;
 155
 156        /*
 157         * The version register is read-only in a real APIC.
 158         */
 159        reg0 = apic_read(APIC_LVR);
 160        Dprintk("Getting VERSION: %x\n", reg0);
 161        apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
 162        reg1 = apic_read(APIC_LVR);
 163        Dprintk("Getting VERSION: %x\n", reg1);
 164
 165        /*
 166         * The two version reads above should print the same
 167         * numbers.  If the second one is different, then we
 168         * poke at a non-APIC.
 169         */
 170        if (reg1 != reg0)
 171                return 0;
 172
 173        /*
 174         * Check if the version looks reasonably.
 175         */
 176        reg1 = GET_APIC_VERSION(reg0);
 177        if (reg1 == 0x00 || reg1 == 0xff)
 178                return 0;
 179        reg1 = get_maxlvt();
 180        if (reg1 < 0x02 || reg1 == 0xff)
 181                return 0;
 182
 183        /*
 184         * The ID register is read/write in a real APIC.
 185         */
 186        reg0 = apic_read(APIC_ID);
 187        Dprintk("Getting ID: %x\n", reg0);
 188        apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
 189        reg1 = apic_read(APIC_ID);
 190        Dprintk("Getting ID: %x\n", reg1);
 191        apic_write(APIC_ID, reg0);
 192        if (reg1 != (reg0 ^ APIC_ID_MASK))
 193                return 0;
 194
 195        /*
 196         * The next two are just to see if we have sane values.
 197         * They're only really relevant if we're in Virtual Wire
 198         * compatibility mode, but most boxes are anymore.
 199         */
 200        reg0 = apic_read(APIC_LVT0);
 201        Dprintk("Getting LVT0: %x\n", reg0);
 202        reg1 = apic_read(APIC_LVT1);
 203        Dprintk("Getting LVT1: %x\n", reg1);
 204
 205        return 1;
 206}
 207
 208void __init sync_Arb_IDs(void)
 209{
 210        /*
 211         * Wait for idle.
 212         */
 213        apic_wait_icr_idle();
 214
 215        Dprintk("Synchronizing Arb IDs.\n");
 216        apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
 217                                | APIC_DM_INIT);
 218}
 219
 220extern void __error_in_apic_c (void);
 221
 222/*
 223 * An initial setup of the virtual wire mode.
 224 */
 225void __init init_bsp_APIC(void)
 226{
 227        unsigned long value, ver;
 228
 229        /*
 230         * Don't do the setup now if we have a SMP BIOS as the
 231         * through-I/O-APIC virtual wire mode might be active.
 232         */
 233        if (smp_found_config || !cpu_has_apic)
 234                return;
 235
 236        value = apic_read(APIC_LVR);
 237        ver = GET_APIC_VERSION(value);
 238
 239        /*
 240         * Do not trust the local APIC being empty at bootup.
 241         */
 242        clear_local_APIC();
 243
 244        /*
 245         * Enable APIC.
 246         */
 247        value = apic_read(APIC_SPIV);
 248        value &= ~APIC_VECTOR_MASK;
 249        value |= APIC_SPIV_APIC_ENABLED;
 250        value |= APIC_SPIV_FOCUS_DISABLED;
 251        value |= SPURIOUS_APIC_VECTOR;
 252        apic_write_around(APIC_SPIV, value);
 253
 254        /*
 255         * Set up the virtual wire mode.
 256         */
 257        apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
 258        value = APIC_DM_NMI;
 259        if (!APIC_INTEGRATED(ver))              /* 82489DX */
 260                value |= APIC_LVT_LEVEL_TRIGGER;
 261        apic_write_around(APIC_LVT1, value);
 262}
 263
 264static unsigned long calculate_ldr(unsigned long old)
 265{
 266        unsigned long id;
 267        if(clustered_apic_mode == CLUSTERED_APIC_XAPIC)
 268                id = physical_to_logical_apicid(hard_smp_processor_id());
 269        else
 270                id = 1UL << smp_processor_id();
 271        return (old & ~APIC_LDR_MASK)|SET_APIC_LOGICAL_ID(id);
 272}
 273
 274void __init setup_local_APIC (void)
 275{
 276        unsigned long value, ver, maxlvt;
 277
 278        /* Pound the ESR really hard over the head with a big hammer - mbligh */
 279        if (esr_disable) {
 280                apic_write(APIC_ESR, 0);
 281                apic_write(APIC_ESR, 0);
 282                apic_write(APIC_ESR, 0);
 283                apic_write(APIC_ESR, 0);
 284        }
 285
 286        value = apic_read(APIC_LVR);
 287        ver = GET_APIC_VERSION(value);
 288
 289        if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
 290                __error_in_apic_c();
 291
 292        /*
 293         * Double-check wether this APIC is really registered.
 294         * This is meaningless in clustered apic mode, so we skip it.
 295         */
 296        if (!clustered_apic_mode && 
 297            !test_bit(GET_APIC_ID(apic_read(APIC_ID)), &phys_cpu_present_map))
 298                BUG();
 299
 300        /*
 301         * Intel recommends to set DFR, LDR and TPR before enabling
 302         * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel
 303         * document number 292116).  So here it goes...
 304         */
 305        if (clustered_apic_mode != CLUSTERED_APIC_NUMAQ) {
 306                /*
 307                 * For NUMA-Q (clustered apic logical), the firmware does this
 308                 * for us. Otherwise put the APIC into clustered or flat
 309                 * delivery mode. Must be "all ones" explicitly for 82489DX.
 310                 */
 311                if(clustered_apic_mode == CLUSTERED_APIC_XAPIC)
 312                        apic_write_around(APIC_DFR, APIC_DFR_CLUSTER);
 313                else
 314                        apic_write_around(APIC_DFR, APIC_DFR_FLAT);
 315
 316                /*
 317                 * Set up the logical destination ID.
 318                 */
 319                value = apic_read(APIC_LDR);
 320                apic_write_around(APIC_LDR, calculate_ldr(value));
 321        }
 322
 323        /*
 324         * Set Task Priority to 'accept all'. We never change this
 325         * later on.
 326         */
 327        value = apic_read(APIC_TASKPRI);
 328        value &= ~APIC_TPRI_MASK;
 329        apic_write_around(APIC_TASKPRI, value);
 330
 331        /*
 332         * Now that we are all set up, enable the APIC
 333         */
 334        value = apic_read(APIC_SPIV);
 335        value &= ~APIC_VECTOR_MASK;
 336        /*
 337         * Enable APIC
 338         */
 339        value |= APIC_SPIV_APIC_ENABLED;
 340
 341        /*
 342         * Some unknown Intel IO/APIC (or APIC) errata is biting us with
 343         * certain networking cards. If high frequency interrupts are
 344         * happening on a particular IOAPIC pin, plus the IOAPIC routing
 345         * entry is masked/unmasked at a high rate as well then sooner or
 346         * later IOAPIC line gets 'stuck', no more interrupts are received
 347         * from the device. If focus CPU is disabled then the hang goes
 348         * away, oh well :-(
 349         *
 350         * [ This bug can be reproduced easily with a level-triggered
 351         *   PCI Ne2000 networking cards and PII/PIII processors, dual
 352         *   BX chipset. ]
 353         */
 354        /*
 355         * Actually disabling the focus CPU check just makes the hang less
 356         * frequent as it makes the interrupt distributon model be more
 357         * like LRU than MRU (the short-term load is more even across CPUs).
 358         * See also the comment in end_level_ioapic_irq().  --macro
 359         */
 360#if 1
 361        /* Enable focus processor (bit==0) */
 362        value &= ~APIC_SPIV_FOCUS_DISABLED;
 363#else
 364        /* Disable focus processor (bit==1) */
 365        value |= APIC_SPIV_FOCUS_DISABLED;
 366#endif
 367        /*
 368         * Set spurious IRQ vector
 369         */
 370        value |= SPURIOUS_APIC_VECTOR;
 371        apic_write_around(APIC_SPIV, value);
 372
 373        /*
 374         * Set up LVT0, LVT1:
 375         *
 376         * set up through-local-APIC on the BP's LINT0. This is not
 377         * strictly necessery in pure symmetric-IO mode, but sometimes
 378         * we delegate interrupts to the 8259A.
 379         */
 380        /*
 381         * TODO: set up through-local-APIC from through-I/O-APIC? --macro
 382         */
 383        value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
 384        if (!smp_processor_id() && (pic_mode || !value)) {
 385                value = APIC_DM_EXTINT;
 386                printk("enabled ExtINT on CPU#%d\n", smp_processor_id());
 387        } else {
 388                value = APIC_DM_EXTINT | APIC_LVT_MASKED;
 389                printk("masked ExtINT on CPU#%d\n", smp_processor_id());
 390        }
 391        apic_write_around(APIC_LVT0, value);
 392
 393        /*
 394         * only the BP should see the LINT1 NMI signal, obviously.
 395         */
 396        if (!smp_processor_id())
 397                value = APIC_DM_NMI;
 398        else
 399                value = APIC_DM_NMI | APIC_LVT_MASKED;
 400        if (!APIC_INTEGRATED(ver))              /* 82489DX */
 401                value |= APIC_LVT_LEVEL_TRIGGER;
 402        apic_write_around(APIC_LVT1, value);
 403
 404        if (APIC_INTEGRATED(ver) && !esr_disable) {             /* !82489DX */
 405                maxlvt = get_maxlvt();
 406                if (maxlvt > 3)         /* Due to the Pentium erratum 3AP. */
 407                        apic_write(APIC_ESR, 0);
 408                value = apic_read(APIC_ESR);
 409                printk("ESR value before enabling vector: %08lx\n", value);
 410
 411                value = ERROR_APIC_VECTOR;      // enables sending errors
 412                apic_write_around(APIC_LVTERR, value);
 413                /*
 414                 * spec says clear errors after enabling vector.
 415                 */
 416                if (maxlvt > 3)
 417                        apic_write(APIC_ESR, 0);
 418                value = apic_read(APIC_ESR);
 419                printk("ESR value after enabling vector: %08lx\n", value);
 420        } else {
 421                if (esr_disable)        
 422                        /* 
 423                         * Something untraceble is creating bad interrupts on 
 424                         * secondary quads ... for the moment, just leave the
 425                         * ESR disabled - we can't do anything useful with the
 426                         * errors anyway - mbligh
 427                         */
 428                        printk("Leaving ESR disabled.\n");
 429                else 
 430                        printk("No ESR for 82489DX.\n");
 431        }
 432
 433        if (nmi_watchdog == NMI_LOCAL_APIC)
 434                setup_apic_nmi_watchdog();
 435}
 436
 437#ifdef CONFIG_PM
 438
 439#include <linux/slab.h>
 440#include <linux/pm.h>
 441
 442static struct {
 443        /* 'active' is true if the local APIC was enabled by us and
 444           not the BIOS; this signifies that we are also responsible
 445           for disabling it before entering apm/acpi suspend */
 446        int active;
 447        /* 'perfctr_pmdev' is here because the current (2.4.1) PM
 448           callback system doesn't handle hierarchical dependencies */
 449        struct pm_dev *perfctr_pmdev;
 450        /* r/w apic fields */
 451        unsigned int apic_id;
 452        unsigned int apic_taskpri;
 453        unsigned int apic_ldr;
 454        unsigned int apic_dfr;
 455        unsigned int apic_spiv;
 456        unsigned int apic_lvtt;
 457        unsigned int apic_lvtpc;
 458        unsigned int apic_lvt0;
 459        unsigned int apic_lvt1;
 460        unsigned int apic_lvterr;
 461        unsigned int apic_tmict;
 462        unsigned int apic_tdcr;
 463} apic_pm_state;
 464
 465static void apic_pm_suspend(void *data)
 466{
 467        unsigned int l, h;
 468        unsigned long flags;
 469
 470        if (apic_pm_state.perfctr_pmdev)
 471                pm_send(apic_pm_state.perfctr_pmdev, PM_SUSPEND, data);
 472        apic_pm_state.apic_id = apic_read(APIC_ID);
 473        apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
 474        apic_pm_state.apic_ldr = apic_read(APIC_LDR);
 475        apic_pm_state.apic_dfr = apic_read(APIC_DFR);
 476        apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
 477        apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
 478        apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
 479        apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
 480        apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
 481        apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
 482        apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
 483        apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
 484        __save_flags(flags);
 485        __cli();
 486        disable_local_APIC();
 487        rdmsr(MSR_IA32_APICBASE, l, h);
 488        l &= ~MSR_IA32_APICBASE_ENABLE;
 489        wrmsr(MSR_IA32_APICBASE, l, h);
 490        __restore_flags(flags);
 491}
 492
 493static void apic_pm_resume(void *data)
 494{
 495        unsigned int l, h;
 496        unsigned long flags;
 497
 498        __save_flags(flags);
 499        __cli();
 500        rdmsr(MSR_IA32_APICBASE, l, h);
 501        l &= ~MSR_IA32_APICBASE_BASE;
 502        l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
 503        wrmsr(MSR_IA32_APICBASE, l, h);
 504        apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
 505        apic_write(APIC_ID, apic_pm_state.apic_id);
 506        apic_write(APIC_DFR, apic_pm_state.apic_dfr);
 507        apic_write(APIC_LDR, apic_pm_state.apic_ldr);
 508        apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
 509        apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
 510        apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
 511        apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
 512        apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
 513        apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
 514        apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
 515        apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
 516        apic_write(APIC_ESR, 0);
 517        apic_read(APIC_ESR);
 518        apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
 519        apic_write(APIC_ESR, 0);
 520        apic_read(APIC_ESR);
 521        __restore_flags(flags);
 522        if (apic_pm_state.perfctr_pmdev)
 523                pm_send(apic_pm_state.perfctr_pmdev, PM_RESUME, data);
 524}
 525
 526static int apic_pm_callback(struct pm_dev *dev, pm_request_t rqst, void *data)
 527{
 528        switch (rqst) {
 529        case PM_SUSPEND:
 530                apic_pm_suspend(data);
 531                break;
 532        case PM_RESUME:
 533                apic_pm_resume(data);
 534                break;
 535        }
 536        return 0;
 537}
 538
 539/* perfctr driver should call this instead of pm_register() */
 540struct pm_dev *apic_pm_register(pm_dev_t type,
 541                                unsigned long id,
 542                                pm_callback callback)
 543{
 544        struct pm_dev *dev;
 545
 546        if (!apic_pm_state.active)
 547                return pm_register(type, id, callback);
 548        if (apic_pm_state.perfctr_pmdev)
 549                return NULL;    /* we're busy */
 550        dev = kmalloc(sizeof(struct pm_dev), GFP_KERNEL);
 551        if (dev) {
 552                memset(dev, 0, sizeof(*dev));
 553                dev->type = type;
 554                dev->id = id;
 555                dev->callback = callback;
 556                apic_pm_state.perfctr_pmdev = dev;
 557        }
 558        return dev;
 559}
 560
 561/* perfctr driver should call this instead of pm_unregister() */
 562void apic_pm_unregister(struct pm_dev *dev)
 563{
 564        if (!apic_pm_state.active) {
 565                pm_unregister(dev);
 566        } else if (dev == apic_pm_state.perfctr_pmdev) {
 567                apic_pm_state.perfctr_pmdev = NULL;
 568                kfree(dev);
 569        }
 570}
 571
 572static void __init apic_pm_init1(void)
 573{
 574        /* can't pm_register() at this early stage in the boot process
 575           (causes an immediate reboot), so just set the flag */
 576        apic_pm_state.active = 1;
 577}
 578
 579static void __init apic_pm_init2(void)
 580{
 581        if (apic_pm_state.active)
 582                pm_register(PM_SYS_DEV, 0, apic_pm_callback);
 583}
 584
 585#else   /* CONFIG_PM */
 586
 587static inline void apic_pm_init1(void) { }
 588static inline void apic_pm_init2(void) { }
 589
 590#endif  /* CONFIG_PM */
 591
 592/*
 593 * Detect and enable local APICs on non-SMP boards.
 594 * Original code written by Keir Fraser.
 595 */
 596int dont_enable_local_apic __initdata = 0;
 597
 598static int __init detect_init_APIC (void)
 599{
 600        u32 h, l, features;
 601        extern void get_cpu_vendor(struct cpuinfo_x86*);
 602
 603        /* Disabled by DMI scan or kernel option? */
 604        if (dont_enable_local_apic)
 605                return -1;
 606
 607        /* Workaround for us being called before identify_cpu(). */
 608        get_cpu_vendor(&boot_cpu_data);
 609
 610        switch (boot_cpu_data.x86_vendor) {
 611        case X86_VENDOR_AMD:
 612                if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1)
 613                        break;
 614                if (boot_cpu_data.x86 == 15 && cpu_has_apic)
 615                        break;
 616                goto no_apic;
 617        case X86_VENDOR_INTEL:
 618                if (boot_cpu_data.x86 == 6 ||
 619                    (boot_cpu_data.x86 == 15 && cpu_has_apic) ||
 620                    (boot_cpu_data.x86 == 5 && cpu_has_apic))
 621                        break;
 622                goto no_apic;
 623        default:
 624                goto no_apic;
 625        }
 626
 627        if (!cpu_has_apic) {
 628                /*
 629                 * Some BIOSes disable the local APIC in the
 630                 * APIC_BASE MSR. This can only be done in
 631                 * software for Intel P6 and AMD K7 (Model > 1).
 632                 */
 633                rdmsr(MSR_IA32_APICBASE, l, h);
 634                if (!(l & MSR_IA32_APICBASE_ENABLE)) {
 635                        printk("Local APIC disabled by BIOS -- reenabling.\n");
 636                        l &= ~MSR_IA32_APICBASE_BASE;
 637                        l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
 638                        wrmsr(MSR_IA32_APICBASE, l, h);
 639                }
 640        }
 641        /*
 642         * The APIC feature bit should now be enabled
 643         * in `cpuid'
 644         */
 645        features = cpuid_edx(1);
 646        if (!(features & (1 << X86_FEATURE_APIC))) {
 647                printk("Could not enable APIC!\n");
 648                return -1;
 649        }
 650        set_bit(X86_FEATURE_APIC, &boot_cpu_data.x86_capability);
 651        mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
 652        if (nmi_watchdog != NMI_NONE)
 653                nmi_watchdog = NMI_LOCAL_APIC;
 654
 655        printk("Found and enabled local APIC!\n");
 656
 657        apic_pm_init1();
 658
 659        return 0;
 660
 661no_apic:
 662        printk("No local APIC present or hardware disabled\n");
 663        return -1;
 664}
 665
 666void __init init_apic_mappings(void)
 667{
 668        unsigned long apic_phys;
 669
 670        /*
 671         * If no local APIC can be found then set up a fake all
 672         * zeroes page to simulate the local APIC and another
 673         * one for the IO-APIC.
 674         */
 675        if (!smp_found_config && detect_init_APIC()) {
 676                apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
 677                apic_phys = __pa(apic_phys);
 678        } else
 679                apic_phys = mp_lapic_addr;
 680
 681        set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
 682        Dprintk("mapped APIC to %08lx (%08lx)\n", APIC_BASE, apic_phys);
 683
 684        /*
 685         * Fetch the APIC ID of the BSP in case we have a
 686         * default configuration (or the MP table is broken).
 687         */
 688        if (boot_cpu_physical_apicid == -1U)
 689                boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
 690
 691#ifdef CONFIG_X86_IO_APIC
 692        {
 693                unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
 694                int i;
 695
 696                for (i = 0; i < nr_ioapics; i++) {
 697                        if (smp_found_config) {
 698                                ioapic_phys = mp_ioapics[i].mpc_apicaddr;
 699                                if (!ioapic_phys) {
 700                                        printk(KERN_ERR "WARNING: bogus zero IO-APIC address found in MPTABLE, disabling IO/APIC support!\n");
 701
 702                                        smp_found_config = 0;
 703                                        skip_ioapic_setup = 1;
 704                                        goto fake_ioapic_page;
 705                                }
 706                        } else {
 707fake_ioapic_page:
 708                                ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
 709                                ioapic_phys = __pa(ioapic_phys);
 710                        }
 711                        set_fixmap_nocache(idx, ioapic_phys);
 712                        Dprintk("mapped IOAPIC to %08lx (%08lx)\n",
 713                                        __fix_to_virt(idx), ioapic_phys);
 714                        idx++;
 715                }
 716        }
 717#endif
 718}
 719
 720/*
 721 * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
 722 * per second. We assume that the caller has already set up the local
 723 * APIC.
 724 *
 725 * The APIC timer is not exactly sync with the external timer chip, it
 726 * closely follows bus clocks.
 727 */
 728
 729/*
 730 * The timer chip is already set up at HZ interrupts per second here,
 731 * but we do not accept timer interrupts yet. We only allow the BP
 732 * to calibrate.
 733 */
 734static unsigned int __init get_8254_timer_count(void)
 735{
 736        extern spinlock_t i8253_lock;
 737        unsigned long flags;
 738
 739        unsigned int count;
 740
 741        spin_lock_irqsave(&i8253_lock, flags);
 742
 743        outb_p(0x00, 0x43);
 744        count = inb_p(0x40);
 745        count |= inb_p(0x40) << 8;
 746
 747        spin_unlock_irqrestore(&i8253_lock, flags);
 748
 749        return count;
 750}
 751
 752void __init wait_8254_wraparound(void)
 753{
 754        unsigned int curr_count, prev_count=~0;
 755        int delta;
 756
 757        curr_count = get_8254_timer_count();
 758
 759        do {
 760                prev_count = curr_count;
 761                curr_count = get_8254_timer_count();
 762                delta = curr_count-prev_count;
 763
 764        /*
 765         * This limit for delta seems arbitrary, but it isn't, it's
 766         * slightly above the level of error a buggy Mercury/Neptune
 767         * chipset timer can cause.
 768         */
 769
 770        } while (delta < 300);
 771}
 772
 773/*
 774 * This function sets up the local APIC timer, with a timeout of
 775 * 'clocks' APIC bus clock. During calibration we actually call
 776 * this function twice on the boot CPU, once with a bogus timeout
 777 * value, second time for real. The other (noncalibrating) CPUs
 778 * call this function only once, with the real, calibrated value.
 779 *
 780 * We do reads before writes even if unnecessary, to get around the
 781 * P5 APIC double write bug.
 782 */
 783
 784#define APIC_DIVISOR 16
 785
 786void __setup_APIC_LVTT(unsigned int clocks)
 787{
 788        unsigned int lvtt1_value, tmp_value;
 789
 790        lvtt1_value = SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV) |
 791                        APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
 792        apic_write_around(APIC_LVTT, lvtt1_value);
 793
 794        /*
 795         * Divide PICLK by 16
 796         */
 797        tmp_value = apic_read(APIC_TDCR);
 798        apic_write_around(APIC_TDCR, (tmp_value
 799                                & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
 800                                | APIC_TDR_DIV_16);
 801
 802        apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
 803}
 804
 805void setup_APIC_timer(void * data)
 806{
 807        unsigned int clocks = (unsigned int) data, slice, t0, t1;
 808        unsigned long flags;
 809        int delta;
 810
 811        __save_flags(flags);
 812        __sti();
 813        /*
 814         * ok, Intel has some smart code in their APIC that knows
 815         * if a CPU was in 'hlt' lowpower mode, and this increases
 816         * its APIC arbitration priority. To avoid the external timer
 817         * IRQ APIC event being in synchron with the APIC clock we
 818         * introduce an interrupt skew to spread out timer events.
 819         *
 820         * The number of slices within a 'big' timeslice is smp_num_cpus+1
 821         */
 822
 823        slice = clocks / (smp_num_cpus+1);
 824        printk("cpu: %d, clocks: %d, slice: %d\n", smp_processor_id(), clocks, slice);
 825
 826        /*
 827         * Wait for IRQ0's slice:
 828         */
 829        wait_8254_wraparound();
 830
 831        __setup_APIC_LVTT(clocks);
 832
 833        t0 = apic_read(APIC_TMICT)*APIC_DIVISOR;
 834        /* Wait till TMCCT gets reloaded from TMICT... */
 835        do {
 836                t1 = apic_read(APIC_TMCCT)*APIC_DIVISOR;
 837                delta = (int)(t0 - t1 - slice*(smp_processor_id()+1));
 838        } while (delta >= 0);
 839        /* Now wait for our slice for real. */
 840        do {
 841                t1 = apic_read(APIC_TMCCT)*APIC_DIVISOR;
 842                delta = (int)(t0 - t1 - slice*(smp_processor_id()+1));
 843        } while (delta < 0);
 844
 845        __setup_APIC_LVTT(clocks);
 846
 847        printk("CPU%d<T0:%d,T1:%d,D:%d,S:%d,C:%d>\n", smp_processor_id(), t0, t1, delta, slice, clocks);
 848
 849        __restore_flags(flags);
 850}
 851
 852/*
 853 * In this function we calibrate APIC bus clocks to the external
 854 * timer. Unfortunately we cannot use jiffies and the timer irq
 855 * to calibrate, since some later bootup code depends on getting
 856 * the first irq? Ugh.
 857 *
 858 * We want to do the calibration only once since we
 859 * want to have local timer irqs syncron. CPUs connected
 860 * by the same APIC bus have the very same bus frequency.
 861 * And we want to have irqs off anyways, no accidental
 862 * APIC irq that way.
 863 */
 864
 865int __init calibrate_APIC_clock(void)
 866{
 867        unsigned long long t1 = 0, t2 = 0;
 868        long tt1, tt2;
 869        long result;
 870        int i;
 871        const int LOOPS = HZ/10;
 872
 873        printk("calibrating APIC timer ...\n");
 874
 875        /*
 876         * Put whatever arbitrary (but long enough) timeout
 877         * value into the APIC clock, we just want to get the
 878         * counter running for calibration.
 879         */
 880        __setup_APIC_LVTT(1000000000);
 881
 882        /*
 883         * The timer chip counts down to zero. Let's wait
 884         * for a wraparound to start exact measurement:
 885         * (the current tick might have been already half done)
 886         */
 887
 888        wait_8254_wraparound();
 889
 890        /*
 891         * We wrapped around just now. Let's start:
 892         */
 893        if (cpu_has_tsc)
 894                rdtscll(t1);
 895        tt1 = apic_read(APIC_TMCCT);
 896
 897        /*
 898         * Let's wait LOOPS wraprounds:
 899         */
 900        for (i = 0; i < LOOPS; i++)
 901                wait_8254_wraparound();
 902
 903        tt2 = apic_read(APIC_TMCCT);
 904        if (cpu_has_tsc)
 905                rdtscll(t2);
 906
 907        /*
 908         * The APIC bus clock counter is 32 bits only, it
 909         * might have overflown, but note that we use signed
 910         * longs, thus no extra care needed.
 911         *
 912         * underflown to be exact, as the timer counts down ;)
 913         */
 914
 915        result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
 916
 917        if (cpu_has_tsc)
 918                printk("..... CPU clock speed is %ld.%04ld MHz.\n",
 919                        ((long)(t2-t1)/LOOPS)/(1000000/HZ),
 920                        ((long)(t2-t1)/LOOPS)%(1000000/HZ));
 921
 922        printk("..... host bus clock speed is %ld.%04ld MHz.\n",
 923                result/(1000000/HZ),
 924                result%(1000000/HZ));
 925
 926        return result;
 927}
 928
 929static unsigned int calibration_result;
 930
 931int dont_use_local_apic_timer __initdata = 0;
 932
 933void __init setup_APIC_clocks (void)
 934{
 935        /* Disabled by DMI scan or kernel option? */
 936        if (dont_use_local_apic_timer)
 937                return;
 938
 939        printk("Using local APIC timer interrupts.\n");
 940        using_apic_timer = 1;
 941
 942        __cli();
 943
 944        calibration_result = calibrate_APIC_clock();
 945        /*
 946         * Now set up the timer for real.
 947         */
 948        setup_APIC_timer((void *)calibration_result);
 949
 950        __sti();
 951
 952        /* and update all other cpus */
 953        smp_call_function(setup_APIC_timer, (void *)calibration_result, 1, 1);
 954}
 955
 956void __init disable_APIC_timer(void)
 957{
 958        if (using_apic_timer) {
 959                unsigned long v;
 960
 961                v = apic_read(APIC_LVTT);
 962                apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
 963        }
 964}
 965
 966void enable_APIC_timer(void)
 967{
 968        if (using_apic_timer) {
 969                unsigned long v;
 970
 971                v = apic_read(APIC_LVTT);
 972                apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
 973        }
 974}
 975
 976/*
 977 * the frequency of the profiling timer can be changed
 978 * by writing a multiplier value into /proc/profile.
 979 */
 980int setup_profiling_timer(unsigned int multiplier)
 981{
 982        int i;
 983
 984        /*
 985         * Sanity check. [at least 500 APIC cycles should be
 986         * between APIC interrupts as a rule of thumb, to avoid
 987         * irqs flooding us]
 988         */
 989        if ( (!multiplier) || (calibration_result/multiplier < 500))
 990                return -EINVAL;
 991
 992        /* 
 993         * Set the new multiplier for each CPU. CPUs don't start using the
 994         * new values until the next timer interrupt in which they do process
 995         * accounting. At that time they also adjust their APIC timers
 996         * accordingly.
 997         */
 998        for (i = 0; i < NR_CPUS; ++i)
 999                prof_multiplier[i] = multiplier;
1000
1001        return 0;
1002}
1003
1004#undef APIC_DIVISOR
1005
1006/*
1007 * Local timer interrupt handler. It does both profiling and
1008 * process statistics/rescheduling.
1009 *
1010 * We do profiling in every local tick, statistics/rescheduling
1011 * happen only every 'profiling multiplier' ticks. The default
1012 * multiplier is 1 and it can be changed by writing the new multiplier
1013 * value into /proc/profile.
1014 */
1015
1016inline void smp_local_timer_interrupt(struct pt_regs * regs)
1017{
1018        int user = user_mode(regs);
1019        int cpu = smp_processor_id();
1020
1021        /*
1022         * The profiling function is SMP safe. (nothing can mess
1023         * around with "current", and the profiling counters are
1024         * updated with atomic operations). This is especially
1025         * useful with a profiling multiplier != 1
1026         */
1027        if (!user)
1028                x86_do_profile(regs->eip);
1029
1030        if (--prof_counter[cpu] <= 0) {
1031                /*
1032                 * The multiplier may have changed since the last time we got
1033                 * to this point as a result of the user writing to
1034                 * /proc/profile. In this case we need to adjust the APIC
1035                 * timer accordingly.
1036                 *
1037                 * Interrupts are already masked off at this point.
1038                 */
1039                prof_counter[cpu] = prof_multiplier[cpu];
1040                if (prof_counter[cpu] != prof_old_multiplier[cpu]) {
1041                        __setup_APIC_LVTT(calibration_result/prof_counter[cpu]);
1042                        prof_old_multiplier[cpu] = prof_counter[cpu];
1043                }
1044
1045#ifdef CONFIG_SMP
1046                update_process_times(user);
1047#endif
1048        }
1049
1050        /*
1051         * We take the 'long' return path, and there every subsystem
1052         * grabs the apropriate locks (kernel lock/ irq lock).
1053         *
1054         * we might want to decouple profiling from the 'long path',
1055         * and do the profiling totally in assembly.
1056         *
1057         * Currently this isn't too much of an issue (performance wise),
1058         * we can take more than 100K local irqs per second on a 100 MHz P5.
1059         */
1060}
1061
1062/*
1063 * Local APIC timer interrupt. This is the most natural way for doing
1064 * local interrupts, but local timer interrupts can be emulated by
1065 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1066 *
1067 * [ if a single-CPU system runs an SMP kernel then we call the local
1068 *   interrupt as well. Thus we cannot inline the local irq ... ]
1069 */
1070unsigned int apic_timer_irqs [NR_CPUS];
1071
1072void smp_apic_timer_interrupt(struct pt_regs * regs)
1073{
1074        int cpu = smp_processor_id();
1075
1076        /*
1077         * the NMI deadlock-detector uses this.
1078         */
1079        apic_timer_irqs[cpu]++;
1080
1081        /*
1082         * NOTE! We'd better ACK the irq immediately,
1083         * because timer handling can be slow.
1084         */
1085        ack_APIC_irq();
1086        /*
1087         * update_process_times() expects us to have done irq_enter().
1088         * Besides, if we don't timer interrupts ignore the global
1089         * interrupt lock, which is the WrongThing (tm) to do.
1090         */
1091        irq_enter(cpu, 0);
1092        smp_local_timer_interrupt(regs);
1093        irq_exit(cpu, 0);
1094
1095        if (softirq_pending(cpu))
1096                do_softirq();
1097}
1098
1099/*
1100 * This interrupt should _never_ happen with our APIC/SMP architecture
1101 */
1102asmlinkage void smp_spurious_interrupt(void)
1103{
1104        unsigned long v;
1105
1106        /*
1107         * Check if this really is a spurious interrupt and ACK it
1108         * if it is a vectored one.  Just in case...
1109         * Spurious interrupts should not be ACKed.
1110         */
1111        v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1112        if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1113                ack_APIC_irq();
1114
1115        /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1116        printk(KERN_INFO "spurious APIC interrupt on CPU#%d, should never happen.\n",
1117                        smp_processor_id());
1118}
1119
1120/*
1121 * This interrupt should never happen with our APIC/SMP architecture
1122 */
1123
1124asmlinkage void smp_error_interrupt(void)
1125{
1126        unsigned long v, v1;
1127
1128        /* First tickle the hardware, only then report what went on. -- REW */
1129        v = apic_read(APIC_ESR);
1130        apic_write(APIC_ESR, 0);
1131        v1 = apic_read(APIC_ESR);
1132        ack_APIC_irq();
1133        atomic_inc(&irq_err_count);
1134
1135        /* Here is what the APIC error bits mean:
1136           0: Send CS error
1137           1: Receive CS error
1138           2: Send accept error
1139           3: Receive accept error
1140           4: Reserved
1141           5: Send illegal vector
1142           6: Received illegal vector
1143           7: Illegal register address
1144        */
1145        printk (KERN_ERR "APIC error on CPU%d: %02lx(%02lx)\n",
1146                smp_processor_id(), v , v1);
1147}
1148
1149/*
1150 * This initializes the IO-APIC and APIC hardware if this is
1151 * a UP kernel.
1152 */
1153int __init APIC_init_uniprocessor (void)
1154{
1155        if (!smp_found_config && !cpu_has_apic)
1156                return -1;
1157
1158        /*
1159         * Complain if the BIOS pretends there is one.
1160         */
1161        if (!cpu_has_apic && APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1162                printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1163                        boot_cpu_physical_apicid);
1164                return -1;
1165        }
1166
1167        verify_local_APIC();
1168
1169        connect_bsp_APIC();
1170
1171        phys_cpu_present_map = 1 << boot_cpu_physical_apicid;
1172
1173        apic_pm_init2();
1174
1175        setup_local_APIC();
1176
1177        if (nmi_watchdog == NMI_LOCAL_APIC)
1178                check_nmi_watchdog();
1179#ifdef CONFIG_X86_IO_APIC
1180        if (smp_found_config)
1181                if (!skip_ioapic_setup && nr_ioapics)
1182                        setup_IO_APIC();
1183#endif
1184        setup_APIC_clocks();
1185
1186        return 0;
1187}
1188
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.