source: trunk/packages/xen-3.1/xen-3.1/xen/arch/x86/apic.c @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

File size: 33.1 KB
Line 
1/*
2 *      based on linux-2.6.17.13/arch/i386/kernel/apic.c
3 *
4 *  Local APIC handling, local APIC timers
5 *
6 *  (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
7 *
8 *  Fixes
9 *  Maciej W. Rozycki   :   Bits for genuine 82489DX APICs;
10 *                  thanks to Eric Gilmore
11 *                  and Rolf G. Tews
12 *                  for testing these extensively.
13 *    Maciej W. Rozycki :   Various updates and fixes.
14 *    Mikael Pettersson :   Power Management for UP-APIC.
15 *    Pavel Machek and
16 *    Mikael Pettersson    :    PM converted to driver model.
17 */
18
19#include <xen/config.h>
20#include <xen/perfc.h>
21#include <xen/errno.h>
22#include <xen/init.h>
23#include <xen/mm.h>
24#include <xen/sched.h>
25#include <xen/irq.h>
26#include <xen/delay.h>
27#include <xen/smp.h>
28#include <xen/softirq.h>
29#include <asm/mc146818rtc.h>
30#include <asm/msr.h>
31#include <asm/atomic.h>
32#include <asm/mpspec.h>
33#include <asm/flushtlb.h>
34#include <asm/hardirq.h>
35#include <asm/apic.h>
36#include <asm/io_apic.h>
37#include <mach_apic.h>
38#include <io_ports.h>
39
40/*
41 * Knob to control our willingness to enable the local APIC.
42 */
43int enable_local_apic __initdata = 0; /* -1=force-disable, +1=force-enable */
44
45/*
46 * Debug level
47 */
48int apic_verbosity;
49
50
51static void apic_pm_activate(void);
52
53int modern_apic(void)
54{
55    unsigned int lvr, version;
56    /* AMD systems use old APIC versions, so check the CPU */
57    if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
58        boot_cpu_data.x86 >= 0xf)
59        return 1;
60    lvr = apic_read(APIC_LVR);
61    version = GET_APIC_VERSION(lvr);
62    return version >= 0x14;
63}
64
65/*
66 * 'what should we do if we get a hw irq event on an illegal vector'.
67 * each architecture has to answer this themselves.
68 */
69void ack_bad_irq(unsigned int irq)
70{
71    printk("unexpected IRQ trap at vector %02x\n", irq);
72    /*
73     * Currently unexpected vectors happen only on SMP and APIC.
74     * We _must_ ack these because every local APIC has only N
75     * irq slots per priority level, and a 'hanging, unacked' IRQ
76     * holds up an irq slot - in excessive cases (when multiple
77     * unexpected vectors occur) that might lock up the APIC
78     * completely.
79     * But only ack when the APIC is enabled -AK
80     */
81    if (cpu_has_apic)
82        ack_APIC_irq();
83}
84
85void __init apic_intr_init(void)
86{
87#ifdef CONFIG_SMP
88    smp_intr_init();
89#endif
90    /* self generated IPI for local APIC timer */
91    set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
92
93    /* IPI vectors for APIC spurious and error interrupts */
94    set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
95    set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
96
97    /* thermal monitor LVT interrupt */
98#ifdef CONFIG_X86_MCE_P4THERMAL
99    set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
100#endif
101}
102
103/* Using APIC to generate smp_local_timer_interrupt? */
104int using_apic_timer = 0;
105
106static int enabled_via_apicbase;
107
108void enable_NMI_through_LVT0 (void * dummy)
109{
110    unsigned int v, ver;
111
112    ver = apic_read(APIC_LVR);
113    ver = GET_APIC_VERSION(ver);
114    v = APIC_DM_NMI;                    /* unmask and set to NMI */
115    if (!APIC_INTEGRATED(ver))          /* 82489DX */
116        v |= APIC_LVT_LEVEL_TRIGGER;
117    apic_write_around(APIC_LVT0, v);
118}
119
120int get_physical_broadcast(void)
121{
122    if (modern_apic())
123        return 0xff;
124    else
125        return 0xf;
126}
127
128int get_maxlvt(void)
129{
130    unsigned int v, ver, maxlvt;
131
132    v = apic_read(APIC_LVR);
133    ver = GET_APIC_VERSION(v);
134    /* 82489DXs do not report # of LVT entries. */
135    maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
136    return maxlvt;
137}
138
139void clear_local_APIC(void)
140{
141    int maxlvt;
142    unsigned long v;
143
144    maxlvt = get_maxlvt();
145
146    /*
147     * Masking an LVT entry on a P6 can trigger a local APIC error
148     * if the vector is zero. Mask LVTERR first to prevent this.
149     */
150    if (maxlvt >= 3) {
151        v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
152        apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
153    }
154    /*
155     * Careful: we have to set masks only first to deassert
156     * any level-triggered sources.
157     */
158    v = apic_read(APIC_LVTT);
159    apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
160    v = apic_read(APIC_LVT0);
161    apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
162    v = apic_read(APIC_LVT1);
163    apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
164    if (maxlvt >= 4) {
165        v = apic_read(APIC_LVTPC);
166        apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
167    }
168
169/* lets not touch this if we didn't frob it */
170#ifdef CONFIG_X86_MCE_P4THERMAL
171    if (maxlvt >= 5) {
172        v = apic_read(APIC_LVTTHMR);
173        apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
174    }
175#endif
176    /*
177     * Clean APIC state for other OSs:
178     */
179    apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
180    apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
181    apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
182    if (maxlvt >= 3)
183        apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
184    if (maxlvt >= 4)
185        apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
186
187#ifdef CONFIG_X86_MCE_P4THERMAL
188    if (maxlvt >= 5)
189        apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
190#endif
191    v = GET_APIC_VERSION(apic_read(APIC_LVR));
192    if (APIC_INTEGRATED(v)) {  /* !82489DX */
193        if (maxlvt > 3)        /* Due to Pentium errata 3AP and 11AP. */
194            apic_write(APIC_ESR, 0);
195        apic_read(APIC_ESR);
196    }
197}
198
199void __init connect_bsp_APIC(void)
200{
201    if (pic_mode) {
202        /*
203         * Do not trust the local APIC being empty at bootup.
204         */
205        clear_local_APIC();
206        /*
207         * PIC mode, enable APIC mode in the IMCR, i.e.
208         * connect BSP's local APIC to INT and NMI lines.
209         */
210        apic_printk(APIC_VERBOSE, "leaving PIC mode, "
211                    "enabling APIC mode.\n");
212        outb(0x70, 0x22);
213        outb(0x01, 0x23);
214    }
215    enable_apic_mode();
216}
217
218void disconnect_bsp_APIC(int virt_wire_setup)
219{
220    if (pic_mode) {
221        /*
222         * Put the board back into PIC mode (has an effect
223         * only on certain older boards).  Note that APIC
224         * interrupts, including IPIs, won't work beyond
225         * this point!  The only exception are INIT IPIs.
226         */
227        apic_printk(APIC_VERBOSE, "disabling APIC mode, "
228                    "entering PIC mode.\n");
229        outb(0x70, 0x22);
230        outb(0x00, 0x23);
231    }
232    else {
233        /* Go back to Virtual Wire compatibility mode */
234        unsigned long value;
235
236        /* For the spurious interrupt use vector F, and enable it */
237        value = apic_read(APIC_SPIV);
238        value &= ~APIC_VECTOR_MASK;
239        value |= APIC_SPIV_APIC_ENABLED;
240        value |= 0xf;
241        apic_write_around(APIC_SPIV, value);
242
243        if (!virt_wire_setup) {
244            /* For LVT0 make it edge triggered, active high, external and enabled */
245            value = apic_read(APIC_LVT0);
246            value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
247                       APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
248                       APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
249            value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
250            value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
251            apic_write_around(APIC_LVT0, value);
252        }
253        else {
254            /* Disable LVT0 */
255            apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
256        }
257
258        /* For LVT1 make it edge triggered, active high, nmi and enabled */
259        value = apic_read(APIC_LVT1);
260        value &= ~(
261            APIC_MODE_MASK | APIC_SEND_PENDING |
262            APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
263            APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
264        value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
265        value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
266        apic_write_around(APIC_LVT1, value);
267    }
268}
269
270void disable_local_APIC(void)
271{
272    unsigned long value;
273
274    clear_local_APIC();
275
276    /*
277     * Disable APIC (implies clearing of registers
278     * for 82489DX!).
279     */
280    value = apic_read(APIC_SPIV);
281    value &= ~APIC_SPIV_APIC_ENABLED;
282    apic_write_around(APIC_SPIV, value);
283
284    if (enabled_via_apicbase) {
285        unsigned int l, h;
286        rdmsr(MSR_IA32_APICBASE, l, h);
287        l &= ~MSR_IA32_APICBASE_ENABLE;
288        wrmsr(MSR_IA32_APICBASE, l, h);
289    }
290}
291
292/*
293 * This is to verify that we're looking at a real local APIC.
294 * Check these against your board if the CPUs aren't getting
295 * started for no apparent reason.
296 */
297int __init verify_local_APIC(void)
298{
299    unsigned int reg0, reg1;
300
301    /*
302     * The version register is read-only in a real APIC.
303     */
304    reg0 = apic_read(APIC_LVR);
305    apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
306    apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
307    reg1 = apic_read(APIC_LVR);
308    apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
309
310    /*
311     * The two version reads above should print the same
312     * numbers.  If the second one is different, then we
313     * poke at a non-APIC.
314     */
315    if (reg1 != reg0)
316        return 0;
317
318    /*
319     * Check if the version looks reasonably.
320     */
321    reg1 = GET_APIC_VERSION(reg0);
322    if (reg1 == 0x00 || reg1 == 0xff)
323        return 0;
324    reg1 = get_maxlvt();
325    if (reg1 < 0x02 || reg1 == 0xff)
326        return 0;
327
328    /*
329     * The ID register is read/write in a real APIC.
330     */
331    reg0 = apic_read(APIC_ID);
332    apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
333
334    /*
335     * The next two are just to see if we have sane values.
336     * They're only really relevant if we're in Virtual Wire
337     * compatibility mode, but most boxes are anymore.
338     */
339    reg0 = apic_read(APIC_LVT0);
340    apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
341    reg1 = apic_read(APIC_LVT1);
342    apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
343
344    return 1;
345}
346
347void __init sync_Arb_IDs(void)
348{
349    /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1
350       And not needed on AMD */
351    if (modern_apic())
352        return;
353    /*
354     * Wait for idle.
355     */
356    apic_wait_icr_idle();
357
358    apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
359    apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
360                      | APIC_DM_INIT);
361}
362
363extern void __error_in_apic_c (void);
364
365/*
366 * An initial setup of the virtual wire mode.
367 */
368void __init init_bsp_APIC(void)
369{
370    unsigned long value, ver;
371
372    /*
373     * Don't do the setup now if we have a SMP BIOS as the
374     * through-I/O-APIC virtual wire mode might be active.
375     */
376    if (smp_found_config || !cpu_has_apic)
377        return;
378
379    value = apic_read(APIC_LVR);
380    ver = GET_APIC_VERSION(value);
381   
382    /*
383     * Do not trust the local APIC being empty at bootup.
384     */
385    clear_local_APIC();
386   
387    /*
388     * Enable APIC.
389     */
390    value = apic_read(APIC_SPIV);
391    value &= ~APIC_VECTOR_MASK;
392    value |= APIC_SPIV_APIC_ENABLED;
393   
394    /* This bit is reserved on P4/Xeon and should be cleared */
395    if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 15))
396        value &= ~APIC_SPIV_FOCUS_DISABLED;
397    else
398        value |= APIC_SPIV_FOCUS_DISABLED;
399    value |= SPURIOUS_APIC_VECTOR;
400    apic_write_around(APIC_SPIV, value);
401
402    /*
403     * Set up the virtual wire mode.
404     */
405    apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
406    value = APIC_DM_NMI;
407    if (!APIC_INTEGRATED(ver))              /* 82489DX */
408        value |= APIC_LVT_LEVEL_TRIGGER;
409    apic_write_around(APIC_LVT1, value);
410}
411
412void __devinit setup_local_APIC(void)
413{
414    unsigned long oldvalue, value, ver, maxlvt;
415    int i, j;
416
417    /* Pound the ESR really hard over the head with a big hammer - mbligh */
418    if (esr_disable) {
419        apic_write(APIC_ESR, 0);
420        apic_write(APIC_ESR, 0);
421        apic_write(APIC_ESR, 0);
422        apic_write(APIC_ESR, 0);
423    }
424
425    value = apic_read(APIC_LVR);
426    ver = GET_APIC_VERSION(value);
427
428    if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
429        __error_in_apic_c();
430
431    /*
432     * Double-check whether this APIC is really registered.
433     */
434    if (!apic_id_registered())
435        BUG();
436
437    /*
438     * Intel recommends to set DFR, LDR and TPR before enabling
439     * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel
440     * document number 292116).  So here it goes...
441     */
442    init_apic_ldr();
443
444    /*
445     * Set Task Priority to 'accept all'. We never change this
446     * later on.
447     */
448    value = apic_read(APIC_TASKPRI);
449    value &= ~APIC_TPRI_MASK;
450    apic_write_around(APIC_TASKPRI, value);
451
452    /*
453     * After a crash, we no longer service the interrupts and a pending
454     * interrupt from previous kernel might still have ISR bit set.
455     *
456     * Most probably by now CPU has serviced that pending interrupt and
457     * it might not have done the ack_APIC_irq() because it thought,
458     * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
459     * does not clear the ISR bit and cpu thinks it has already serivced
460     * the interrupt. Hence a vector might get locked. It was noticed
461     * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
462     */
463    for (i = APIC_ISR_NR - 1; i >= 0; i--) {
464        value = apic_read(APIC_ISR + i*0x10);
465        for (j = 31; j >= 0; j--) {
466            if (value & (1<<j))
467                ack_APIC_irq();
468        }
469    }
470
471    /*
472     * Now that we are all set up, enable the APIC
473     */
474    value = apic_read(APIC_SPIV);
475    value &= ~APIC_VECTOR_MASK;
476    /*
477     * Enable APIC
478     */
479    value |= APIC_SPIV_APIC_ENABLED;
480
481    /*
482     * Some unknown Intel IO/APIC (or APIC) errata is biting us with
483     * certain networking cards. If high frequency interrupts are
484     * happening on a particular IOAPIC pin, plus the IOAPIC routing
485     * entry is masked/unmasked at a high rate as well then sooner or
486     * later IOAPIC line gets 'stuck', no more interrupts are received
487     * from the device. If focus CPU is disabled then the hang goes
488     * away, oh well :-(
489     *
490     * [ This bug can be reproduced easily with a level-triggered
491     *   PCI Ne2000 networking cards and PII/PIII processors, dual
492     *   BX chipset. ]
493     */
494    /*
495     * Actually disabling the focus CPU check just makes the hang less
496     * frequent as it makes the interrupt distributon model be more
497     * like LRU than MRU (the short-term load is more even across CPUs).
498     * See also the comment in end_level_ioapic_irq().  --macro
499     */
500#if 1
501    /* Enable focus processor (bit==0) */
502    value &= ~APIC_SPIV_FOCUS_DISABLED;
503#else
504    /* Disable focus processor (bit==1) */
505    value |= APIC_SPIV_FOCUS_DISABLED;
506#endif
507    /*
508     * Set spurious IRQ vector
509     */
510    value |= SPURIOUS_APIC_VECTOR;
511    apic_write_around(APIC_SPIV, value);
512
513    /*
514     * Set up LVT0, LVT1:
515     *
516     * set up through-local-APIC on the BP's LINT0. This is not
517     * strictly necessery in pure symmetric-IO mode, but sometimes
518     * we delegate interrupts to the 8259A.
519     */
520    /*
521     * TODO: set up through-local-APIC from through-I/O-APIC? --macro
522     */
523    value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
524    if (!smp_processor_id() && (pic_mode || !value)) {
525        value = APIC_DM_EXTINT;
526        apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
527                    smp_processor_id());
528    } else {
529        value = APIC_DM_EXTINT | APIC_LVT_MASKED;
530        apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
531                    smp_processor_id());
532    }
533    apic_write_around(APIC_LVT0, value);
534
535    /*
536     * only the BP should see the LINT1 NMI signal, obviously.
537     */
538    if (!smp_processor_id())
539        value = APIC_DM_NMI;
540    else
541        value = APIC_DM_NMI | APIC_LVT_MASKED;
542    if (!APIC_INTEGRATED(ver))      /* 82489DX */
543        value |= APIC_LVT_LEVEL_TRIGGER;
544    apic_write_around(APIC_LVT1, value);
545
546    if (APIC_INTEGRATED(ver) && !esr_disable) {        /* !82489DX */
547        maxlvt = get_maxlvt();
548        if (maxlvt > 3)     /* Due to the Pentium erratum 3AP. */
549            apic_write(APIC_ESR, 0);
550        oldvalue = apic_read(APIC_ESR);
551
552        value = ERROR_APIC_VECTOR;      // enables sending errors
553        apic_write_around(APIC_LVTERR, value);
554        /*
555         * spec says clear errors after enabling vector.
556         */
557        if (maxlvt > 3)
558            apic_write(APIC_ESR, 0);
559        value = apic_read(APIC_ESR);
560        if (value != oldvalue)
561            apic_printk(APIC_VERBOSE, "ESR value before enabling "
562                        "vector: 0x%08lx  after: 0x%08lx\n",
563                        oldvalue, value);
564    } else {
565        if (esr_disable)   
566            /*
567             * Something untraceble is creating bad interrupts on
568             * secondary quads ... for the moment, just leave the
569             * ESR disabled - we can't do anything useful with the
570             * errors anyway - mbligh
571             */
572            printk("Leaving ESR disabled.\n");
573        else
574            printk("No ESR for 82489DX.\n");
575    }
576
577    if (nmi_watchdog == NMI_LOCAL_APIC)
578        setup_apic_nmi_watchdog();
579    apic_pm_activate();
580}
581
582/*
583 * If Linux enabled the LAPIC against the BIOS default
584 * disable it down before re-entering the BIOS on shutdown.
585 * Otherwise the BIOS may get confused and not power-off.
586 * Additionally clear all LVT entries before disable_local_APIC
587 * for the case where Linux didn't enable the LAPIC.
588 */
589void lapic_shutdown(void)
590{
591    unsigned long flags;
592
593    if (!cpu_has_apic)
594        return;
595
596    local_irq_save(flags);
597    clear_local_APIC();
598
599    if (enabled_via_apicbase)
600        disable_local_APIC();
601
602    local_irq_restore(flags);
603}
604
605static void apic_pm_activate(void) { }
606
607/*
608 * Detect and enable local APICs on non-SMP boards.
609 * Original code written by Keir Fraser.
610 */
611
612static void __init lapic_disable(char *str)
613{
614    enable_local_apic = -1;
615    clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
616}
617custom_param("nolapic", lapic_disable);
618
619static void __init lapic_enable(char *str)
620{
621    enable_local_apic = 1;
622}
623custom_param("lapic", lapic_enable);
624
625static void __init apic_set_verbosity(char *str)
626{
627    if (strcmp("debug", str) == 0)
628        apic_verbosity = APIC_DEBUG;
629    else if (strcmp("verbose", str) == 0)
630        apic_verbosity = APIC_VERBOSE;
631    else
632        printk(KERN_WARNING "APIC Verbosity level %s not recognised"
633               " use apic_verbosity=verbose or apic_verbosity=debug", str);
634}
635custom_param("apic_verbosity", apic_set_verbosity);
636
637static int __init detect_init_APIC (void)
638{
639    u32 h, l, features;
640
641    /* Disabled by kernel option? */
642    if (enable_local_apic < 0)
643        return -1;
644
645    switch (boot_cpu_data.x86_vendor) {
646    case X86_VENDOR_AMD:
647        if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
648            (boot_cpu_data.x86 == 15))       
649            break;
650        goto no_apic;
651    case X86_VENDOR_INTEL:
652        if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
653            (boot_cpu_data.x86 == 5 && cpu_has_apic))
654            break;
655        goto no_apic;
656    default:
657        goto no_apic;
658    }
659
660    if (!cpu_has_apic) {
661        /*
662         * Over-ride BIOS and try to enable the local
663         * APIC only if "lapic" specified.
664         */
665        if (enable_local_apic <= 0) {
666            printk("Local APIC disabled by BIOS -- "
667                   "you can enable it with \"lapic\"\n");
668            return -1;
669        }
670        /*
671         * Some BIOSes disable the local APIC in the
672         * APIC_BASE MSR. This can only be done in
673         * software for Intel P6 or later and AMD K7
674         * (Model > 1) or later.
675         */
676        rdmsr(MSR_IA32_APICBASE, l, h);
677        if (!(l & MSR_IA32_APICBASE_ENABLE)) {
678            printk("Local APIC disabled by BIOS -- reenabling.\n");
679            l &= ~MSR_IA32_APICBASE_BASE;
680            l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
681            wrmsr(MSR_IA32_APICBASE, l, h);
682            enabled_via_apicbase = 1;
683        }
684    }
685    /*
686     * The APIC feature bit should now be enabled
687     * in `cpuid'
688     */
689    features = cpuid_edx(1);
690    if (!(features & (1 << X86_FEATURE_APIC))) {
691        printk("Could not enable APIC!\n");
692        return -1;
693    }
694
695    set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
696    mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
697
698    /* The BIOS may have set up the APIC at some other address */
699    rdmsr(MSR_IA32_APICBASE, l, h);
700    if (l & MSR_IA32_APICBASE_ENABLE)
701        mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
702
703    if (nmi_watchdog != NMI_NONE)
704        nmi_watchdog = NMI_LOCAL_APIC;
705
706    printk("Found and enabled local APIC!\n");
707
708    apic_pm_activate();
709
710    return 0;
711
712no_apic:
713    printk("No local APIC present or hardware disabled\n");
714    return -1;
715}
716
717void __init init_apic_mappings(void)
718{
719    unsigned long apic_phys;
720
721    /*
722     * If no local APIC can be found then set up a fake all
723     * zeroes page to simulate the local APIC and another
724     * one for the IO-APIC.
725     */
726    if (!smp_found_config && detect_init_APIC()) {
727        apic_phys = __pa(alloc_xenheap_page());
728        memset(__va(apic_phys), 0, PAGE_SIZE);
729    } else
730        apic_phys = mp_lapic_addr;
731
732    set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
733    apic_printk(APIC_VERBOSE, "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
734                apic_phys);
735
736    /*
737     * Fetch the APIC ID of the BSP in case we have a
738     * default configuration (or the MP table is broken).
739     */
740    if (boot_cpu_physical_apicid == -1U)
741        boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
742
743#ifdef CONFIG_X86_IO_APIC
744    {
745        unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
746        int i;
747
748        for (i = 0; i < nr_ioapics; i++) {
749            if (smp_found_config) {
750                ioapic_phys = mp_ioapics[i].mpc_apicaddr;
751                if (!ioapic_phys) {
752                    printk(KERN_ERR
753                           "WARNING: bogus zero IO-APIC "
754                           "address found in MPTABLE, "
755                           "disabling IO/APIC support!\n");
756                    smp_found_config = 0;
757                    skip_ioapic_setup = 1;
758                    goto fake_ioapic_page;
759                }
760            } else {
761fake_ioapic_page:
762                ioapic_phys = __pa(alloc_xenheap_page());
763                memset(__va(ioapic_phys), 0, PAGE_SIZE);
764            }
765            set_fixmap_nocache(idx, ioapic_phys);
766            apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08lx (%08lx)\n",
767                        __fix_to_virt(idx), ioapic_phys);
768            idx++;
769        }
770    }
771#endif
772}
773
774/*****************************************************************************
775 * APIC calibration
776 *
777 * The APIC is programmed in bus cycles.
778 * Timeout values should specified in real time units.
779 * The "cheapest" time source is the cyclecounter.
780 *
781 * Thus, we need a mappings from: bus cycles <- cycle counter <- system time
782 *
783 * The calibration is currently a bit shoddy since it requires the external
784 * timer chip to generate periodic timer interupts.
785 *****************************************************************************/
786
787/* used for system time scaling */
788static unsigned long bus_freq;    /* KAF: pointer-size avoids compile warns. */
789static u32           bus_cycle;   /* length of one bus cycle in pico-seconds */
790static u32           bus_scale;   /* scaling factor convert ns to bus cycles */
791
792/*
793 * The timer chip is already set up at HZ interrupts per second here,
794 * but we do not accept timer interrupts yet. We only allow the BP
795 * to calibrate.
796 */
797static unsigned int __init get_8254_timer_count(void)
798{
799    /*extern spinlock_t i8253_lock;*/
800    /*unsigned long flags;*/
801
802    unsigned int count;
803
804    /*spin_lock_irqsave(&i8253_lock, flags);*/
805
806    outb_p(0x00, PIT_MODE);
807    count = inb_p(PIT_CH0);
808    count |= inb_p(PIT_CH0) << 8;
809
810    /*spin_unlock_irqrestore(&i8253_lock, flags);*/
811
812    return count;
813}
814
815/* next tick in 8254 can be caught by catching timer wraparound */
816static void __init wait_8254_wraparound(void)
817{
818    unsigned int curr_count, prev_count;
819   
820    curr_count = get_8254_timer_count();
821    do {
822        prev_count = curr_count;
823        curr_count = get_8254_timer_count();
824
825        /* workaround for broken Mercury/Neptune */
826        if (prev_count >= curr_count + 0x100)
827            curr_count = get_8254_timer_count();
828       
829    } while (prev_count >= curr_count);
830}
831
832/*
833 * Default initialization for 8254 timers. If we use other timers like HPET,
834 * we override this later
835 */
836void (*wait_timer_tick)(void) __initdata = wait_8254_wraparound;
837
838/*
839 * This function sets up the local APIC timer, with a timeout of
840 * 'clocks' APIC bus clock. During calibration we actually call
841 * this function twice on the boot CPU, once with a bogus timeout
842 * value, second time for real. The other (noncalibrating) CPUs
843 * call this function only once, with the real, calibrated value.
844 *
845 * We do reads before writes even if unnecessary, to get around the
846 * P5 APIC double write bug.
847 */
848
849#define APIC_DIVISOR 1
850
851void __setup_APIC_LVTT(unsigned int clocks)
852{
853    unsigned int lvtt_value, tmp_value, ver;
854
855    ver = GET_APIC_VERSION(apic_read(APIC_LVR));
856    /* NB. Xen uses local APIC timer in one-shot mode. */
857    lvtt_value = /*APIC_LVT_TIMER_PERIODIC |*/ LOCAL_TIMER_VECTOR;
858    if (!APIC_INTEGRATED(ver))
859        lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
860    apic_write_around(APIC_LVTT, lvtt_value);
861
862    tmp_value = apic_read(APIC_TDCR);
863    apic_write_around(APIC_TDCR, (tmp_value | APIC_TDR_DIV_1));
864
865    apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
866}
867
868static void __init setup_APIC_timer(unsigned int clocks)
869{
870    unsigned long flags;
871    local_irq_save(flags);
872    __setup_APIC_LVTT(clocks);
873    local_irq_restore(flags);
874}
875
876/*
877 * In this function we calibrate APIC bus clocks to the external
878 * timer. Unfortunately we cannot use jiffies and the timer irq
879 * to calibrate, since some later bootup code depends on getting
880 * the first irq? Ugh.
881 *
882 * We want to do the calibration only once since we
883 * want to have local timer irqs syncron. CPUs connected
884 * by the same APIC bus have the very same bus frequency.
885 * And we want to have irqs off anyways, no accidental
886 * APIC irq that way.
887 */
888
889int __init calibrate_APIC_clock(void)
890{
891    unsigned long long t1 = 0, t2 = 0;
892    long tt1, tt2;
893    long result;
894    int i;
895    const int LOOPS = HZ/10;
896
897    apic_printk(APIC_VERBOSE, "calibrating APIC timer ...\n");
898
899    /*
900     * Put whatever arbitrary (but long enough) timeout
901     * value into the APIC clock, we just want to get the
902     * counter running for calibration.
903     */
904    __setup_APIC_LVTT(1000000000);
905
906    /*
907     * The timer chip counts down to zero. Let's wait
908     * for a wraparound to start exact measurement:
909     * (the current tick might have been already half done)
910     */
911    wait_timer_tick();
912
913    /*
914     * We wrapped around just now. Let's start:
915     */
916    if (cpu_has_tsc)
917        rdtscll(t1);
918    tt1 = apic_read(APIC_TMCCT);
919
920    /*
921     * Let's wait LOOPS wraprounds:
922     */
923    for (i = 0; i < LOOPS; i++)
924        wait_timer_tick();
925
926    tt2 = apic_read(APIC_TMCCT);
927    if (cpu_has_tsc)
928        rdtscll(t2);
929
930    /*
931     * The APIC bus clock counter is 32 bits only, it
932     * might have overflown, but note that we use signed
933     * longs, thus no extra care needed.
934     *
935     * underflown to be exact, as the timer counts down ;)
936     */
937
938    result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
939
940    if (cpu_has_tsc)
941        apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
942                    "%ld.%04ld MHz.\n",
943                    ((long)(t2-t1)/LOOPS)/(1000000/HZ),
944                    ((long)(t2-t1)/LOOPS)%(1000000/HZ));
945
946    apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
947                "%ld.%04ld MHz.\n",
948                result/(1000000/HZ),
949                result%(1000000/HZ));
950
951    /* set up multipliers for accurate timer code */
952    bus_freq   = result*HZ;
953    bus_cycle  = (u32) (1000000000000LL/bus_freq); /* in pico seconds */
954    bus_scale  = (1000*262144)/bus_cycle;
955
956    apic_printk(APIC_VERBOSE, "..... bus_scale = 0x%08X\n", bus_scale);
957    /* reset APIC to zero timeout value */
958    __setup_APIC_LVTT(0);
959
960    return result;
961}
962
963u32 get_apic_bus_cycle(void)
964{
965    return bus_cycle;
966}
967
968static unsigned int calibration_result;
969
970void __init setup_boot_APIC_clock(void)
971{
972    unsigned long flags;
973    apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n");
974    using_apic_timer = 1;
975
976    local_irq_save(flags);
977
978    calibration_result = calibrate_APIC_clock();
979    /*
980     * Now set up the timer for real.
981     */
982    setup_APIC_timer(calibration_result);
983   
984    local_irq_restore(flags);
985}
986
987void __devinit setup_secondary_APIC_clock(void)
988{
989    setup_APIC_timer(calibration_result);
990}
991
992void disable_APIC_timer(void)
993{
994    if (using_apic_timer) {
995        unsigned long v;
996       
997        v = apic_read(APIC_LVTT);
998        apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
999    }
1000}
1001
1002void enable_APIC_timer(void)
1003{
1004    if (using_apic_timer) {
1005        unsigned long v;
1006       
1007        v = apic_read(APIC_LVTT);
1008        apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
1009    }
1010}
1011
1012#undef APIC_DIVISOR
1013
1014/*
1015 * reprogram the APIC timer. Timeoutvalue is in ns from start of boot
1016 * returns 1 on success
1017 * returns 0 if the timeout value is too small or in the past.
1018 */
1019int reprogram_timer(s_time_t timeout)
1020{
1021    s_time_t    now;
1022    s_time_t    expire;
1023    u64         apic_tmict;
1024
1025    /*
1026     * If we don't have local APIC then we just poll the timer list off the
1027     * PIT interrupt.
1028     */
1029    if ( !cpu_has_apic )
1030        return 1;
1031
1032    /*
1033     * We use this value because we don't trust zero (we think it may just
1034     * cause an immediate interrupt). At least this is guaranteed to hold it
1035     * off for ages (esp. since the clock ticks on bus clock, not cpu clock!).
1036     */
1037    if ( timeout == 0 )
1038    {
1039        apic_tmict = 0xffffffff;
1040        goto reprogram;
1041    }
1042
1043    now = NOW();
1044    expire = timeout - now; /* value from now */
1045
1046    if ( expire <= 0 )
1047    {
1048        Dprintk("APICT[%02d] Timeout in the past 0x%08X%08X > 0x%08X%08X\n", 
1049                smp_processor_id(), (u32)(now>>32), 
1050                (u32)now, (u32)(timeout>>32),(u32)timeout);
1051        return 0;
1052    }
1053
1054    /* conversion to bus units */
1055    apic_tmict = (((u64)bus_scale) * expire)>>18;
1056
1057    if ( apic_tmict >= 0xffffffff )
1058    {
1059        Dprintk("APICT[%02d] Timeout value too large\n", smp_processor_id());
1060        apic_tmict = 0xffffffff;
1061    }
1062
1063    if ( apic_tmict == 0 )
1064    {
1065        Dprintk("APICT[%02d] timeout value too small\n", smp_processor_id());
1066        return 0;
1067    }
1068
1069 reprogram:
1070    /* Program the timer. */
1071    apic_write(APIC_TMICT, (unsigned long)apic_tmict);
1072
1073    return 1;
1074}
1075
1076fastcall void smp_apic_timer_interrupt(struct cpu_user_regs * regs)
1077{
1078    ack_APIC_irq();
1079    perfc_incr(apic_timer);
1080    raise_softirq(TIMER_SOFTIRQ);
1081}
1082
1083/*
1084 * This interrupt should _never_ happen with our APIC/SMP architecture
1085 */
1086fastcall void smp_spurious_interrupt(struct cpu_user_regs *regs)
1087{
1088    unsigned long v;
1089
1090    irq_enter();
1091    /*
1092     * Check if this really is a spurious interrupt and ACK it
1093     * if it is a vectored one.  Just in case...
1094     * Spurious interrupts should not be ACKed.
1095     */
1096    v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1097    if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1098        ack_APIC_irq();
1099
1100    /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1101    printk(KERN_INFO "spurious APIC interrupt on CPU#%d, should never happen.\n",
1102           smp_processor_id());
1103    irq_exit();
1104}
1105
1106/*
1107 * This interrupt should never happen with our APIC/SMP architecture
1108 */
1109
1110fastcall void smp_error_interrupt(struct cpu_user_regs *regs)
1111{
1112    unsigned long v, v1;
1113
1114    irq_enter();
1115    /* First tickle the hardware, only then report what went on. -- REW */
1116    v = apic_read(APIC_ESR);
1117    apic_write(APIC_ESR, 0);
1118    v1 = apic_read(APIC_ESR);
1119    ack_APIC_irq();
1120    atomic_inc(&irq_err_count);
1121
1122    /* Here is what the APIC error bits mean:
1123       0: Send CS error
1124       1: Receive CS error
1125       2: Send accept error
1126       3: Receive accept error
1127       4: Reserved
1128       5: Send illegal vector
1129       6: Received illegal vector
1130       7: Illegal register address
1131    */
1132    printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1133            smp_processor_id(), v , v1);
1134    irq_exit();
1135}
1136
1137/*
1138 * This initializes the IO-APIC and APIC hardware if this is
1139 * a UP kernel.
1140 */
1141int __init APIC_init_uniprocessor (void)
1142{
1143    if (enable_local_apic < 0)
1144        clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1145
1146    if (!smp_found_config && !cpu_has_apic)
1147        return -1;
1148
1149    /*
1150     * Complain if the BIOS pretends there is one.
1151     */
1152    if (!cpu_has_apic && APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1153        printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1154               boot_cpu_physical_apicid);
1155        clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1156        return -1;
1157    }
1158
1159    verify_local_APIC();
1160
1161    connect_bsp_APIC();
1162
1163    /*
1164     * Hack: In case of kdump, after a crash, kernel might be booting
1165     * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1166     * might be zero if read from MP tables. Get it from LAPIC.
1167     */
1168#ifdef CONFIG_CRASH_DUMP
1169    boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1170#endif
1171    phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1172
1173    setup_local_APIC();
1174
1175    if (nmi_watchdog == NMI_LOCAL_APIC)
1176        check_nmi_watchdog();
1177#ifdef CONFIG_X86_IO_APIC
1178    if (smp_found_config)
1179        if (!skip_ioapic_setup && nr_ioapics)
1180            setup_IO_APIC();
1181#endif
1182    setup_boot_APIC_clock();
1183
1184    return 0;
1185}
Note: See TracBrowser for help on using the repository browser.