~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to arch/x86/kernel/apic/apic.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <linux/ftrace.h>
25
25
#include <linux/ioport.h>
26
26
#include <linux/module.h>
27
 
#include <linux/sysdev.h>
 
27
#include <linux/syscore_ops.h>
28
28
#include <linux/delay.h>
29
29
#include <linux/timex.h>
30
30
#include <linux/dmar.h>
33
33
#include <linux/dmi.h>
34
34
#include <linux/smp.h>
35
35
#include <linux/mm.h>
36
 
#include <trace/irq.h>
37
36
 
38
37
#include <asm/perf_event.h>
39
38
#include <asm/x86_init.h>
44
43
#include <asm/i8259.h>
45
44
#include <asm/proto.h>
46
45
#include <asm/apic.h>
 
46
#include <asm/io_apic.h>
47
47
#include <asm/desc.h>
48
48
#include <asm/hpet.h>
49
49
#include <asm/idle.h>
79
79
EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
80
80
 
81
81
#ifdef CONFIG_X86_32
 
82
 
 
83
/*
 
84
 * On x86_32, the mapping between cpu and logical apicid may vary
 
85
 * depending on apic in use.  The following early percpu variable is
 
86
 * used for the mapping.  This is where the behaviors of x86_64 and 32
 
87
 * actually diverge.  Let's keep it ugly for now.
 
88
 */
 
89
DEFINE_EARLY_PER_CPU(int, x86_cpu_to_logical_apicid, BAD_APICID);
 
90
 
82
91
/*
83
92
 * Knob to control our willingness to enable the local APIC.
84
93
 *
85
94
 * +1=force-enable
86
95
 */
87
 
static int force_enable_local_apic;
 
96
static int force_enable_local_apic __initdata;
88
97
/*
89
98
 * APIC command line parameters
90
99
 */
154
163
unsigned long mp_lapic_addr;
155
164
int disable_apic;
156
165
/* Disable local APIC timer from the kernel commandline or via dmi quirk */
157
 
static int disable_apic_timer __cpuinitdata;
 
166
static int disable_apic_timer __initdata;
158
167
/* Local APIC timer works in C2 */
159
168
int local_apic_timer_c2_ok;
160
169
EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
178
187
 
179
188
static unsigned int calibration_result;
180
189
 
181
 
static int lapic_next_event(unsigned long delta,
182
 
                            struct clock_event_device *evt);
183
 
static void lapic_timer_setup(enum clock_event_mode mode,
184
 
                              struct clock_event_device *evt);
185
 
static void lapic_timer_broadcast(const struct cpumask *mask);
186
190
static void apic_pm_activate(void);
187
191
 
188
 
/*
189
 
 * The local apic timer can be used for any function which is CPU local.
190
 
 */
191
 
static struct clock_event_device lapic_clockevent = {
192
 
        .name           = "lapic",
193
 
        .features       = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
194
 
                        | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
195
 
        .shift          = 32,
196
 
        .set_mode       = lapic_timer_setup,
197
 
        .set_next_event = lapic_next_event,
198
 
        .broadcast      = lapic_timer_broadcast,
199
 
        .rating         = 100,
200
 
        .irq            = -1,
201
 
};
202
 
static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
203
 
 
204
192
static unsigned long apic_phys;
205
193
 
206
194
/*
239
227
 * right after this call apic become NOOP driven
240
228
 * so apic->write/read doesn't do anything
241
229
 */
242
 
void apic_disable(void)
 
230
static void __init apic_disable(void)
243
231
{
244
232
        pr_info("APIC: switched to apic NOOP\n");
245
233
        apic = &apic_noop;
283
271
        return icr1 | ((u64)icr2 << 32);
284
272
}
285
273
 
286
 
/**
287
 
 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
288
 
 */
289
 
void __cpuinit enable_NMI_through_LVT0(void)
290
 
{
291
 
        unsigned int v;
292
 
 
293
 
        /* unmask and set to NMI */
294
 
        v = APIC_DM_NMI;
295
 
 
296
 
        /* Level triggered for 82489DX (32bit mode) */
297
 
        if (!lapic_is_integrated())
298
 
                v |= APIC_LVT_LEVEL_TRIGGER;
299
 
 
300
 
        apic_write(APIC_LVT0, v);
301
 
}
302
 
 
303
274
#ifdef CONFIG_X86_32
304
275
/**
305
276
 * get_physical_broadcast - Get number of physical broadcast IDs
419
390
 
420
391
/*
421
392
 * If mask=1, the LVT entry does not generate interrupts while mask=0
422
 
 * enables the vector. See also the BKDGs.
 
393
 * enables the vector. See also the BKDGs. Must be called with
 
394
 * preemption disabled.
423
395
 */
424
396
 
425
397
int setup_APIC_eilvt(u8 offset, u8 vector, u8 msg_type, u8 mask)
509
481
#endif
510
482
}
511
483
 
 
484
 
 
485
/*
 
486
 * The local apic timer can be used for any function which is CPU local.
 
487
 */
 
488
static struct clock_event_device lapic_clockevent = {
 
489
        .name           = "lapic",
 
490
        .features       = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
 
491
                        | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
 
492
        .shift          = 32,
 
493
        .set_mode       = lapic_timer_setup,
 
494
        .set_next_event = lapic_next_event,
 
495
        .broadcast      = lapic_timer_broadcast,
 
496
        .rating         = 100,
 
497
        .irq            = -1,
 
498
};
 
499
static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
 
500
 
512
501
/*
513
502
 * Setup the local APIC timer for this CPU. Copy the initialized values
514
503
 * of the boot CPU and register the clock event in the framework.
517
506
{
518
507
        struct clock_event_device *levt = &__get_cpu_var(lapic_events);
519
508
 
520
 
        if (cpu_has(__this_cpu_ptr(&cpu_info), X86_FEATURE_ARAT)) {
 
509
        if (this_cpu_has(X86_FEATURE_ARAT)) {
521
510
                lapic_clockevent.features &= ~CLOCK_EVT_FEAT_C3STOP;
522
511
                /* Make LAPIC timer preferrable over percpu HPET */
523
512
                lapic_clockevent.rating = 150;
869
858
         */
870
859
        exit_idle();
871
860
        irq_enter();
872
 
        trace_irq_entry(LOCAL_TIMER_VECTOR, regs, NULL);
873
861
        local_apic_timer_interrupt();
874
 
        trace_irq_exit(IRQ_HANDLED);
875
862
        irq_exit();
876
863
 
877
864
        set_irq_regs(old_regs);
1212
1199
                rdtscll(tsc);
1213
1200
 
1214
1201
        if (disable_apic) {
1215
 
                arch_disable_smp_support();
 
1202
                disable_ioapic_support();
1216
1203
                return;
1217
1204
        }
1218
1205
 
1240
1227
         */
1241
1228
        apic->init_apic_ldr();
1242
1229
 
 
1230
#ifdef CONFIG_X86_32
 
1231
        /*
 
1232
         * APIC LDR is initialized.  If logical_apicid mapping was
 
1233
         * initialized during get_smp_config(), make sure it matches the
 
1234
         * actual value.
 
1235
         */
 
1236
        i = early_per_cpu(x86_cpu_to_logical_apicid, cpu);
 
1237
        WARN_ON(i != BAD_APICID && i != logical_smp_processor_id());
 
1238
        /* always use the value from LDR */
 
1239
        early_per_cpu(x86_cpu_to_logical_apicid, cpu) =
 
1240
                logical_smp_processor_id();
 
1241
 
 
1242
        /*
 
1243
         * Some NUMA implementations (NUMAQ) don't initialize apicid to
 
1244
         * node mapping during NUMA init.  Now that logical apicid is
 
1245
         * guaranteed to be known, give it another chance.  This is already
 
1246
         * a bit too late - percpu allocation has already happened without
 
1247
         * proper NUMA affinity.
 
1248
         */
 
1249
        if (apic->x86_32_numa_cpu_node)
 
1250
                set_apicid_to_node(early_per_cpu(x86_cpu_to_apicid, cpu),
 
1251
                                   apic->x86_32_numa_cpu_node(cpu));
 
1252
#endif
 
1253
 
1243
1254
        /*
1244
1255
         * Set Task Priority to 'accept all'. We never change this
1245
1256
         * later on.
1451
1462
void __init enable_IR_x2apic(void)
1452
1463
{
1453
1464
        unsigned long flags;
1454
 
        struct IO_APIC_route_entry **ioapic_entries = NULL;
1455
1465
        int ret, x2apic_enabled = 0;
1456
1466
        int dmar_table_init_ret;
1457
1467
 
1459
1469
        if (dmar_table_init_ret && !x2apic_supported())
1460
1470
                return;
1461
1471
 
1462
 
        ioapic_entries = alloc_ioapic_entries();
1463
 
        if (!ioapic_entries) {
1464
 
                pr_err("Allocate ioapic_entries failed\n");
1465
 
                goto out;
1466
 
        }
1467
 
 
1468
 
        ret = save_IO_APIC_setup(ioapic_entries);
 
1472
        ret = save_ioapic_entries();
1469
1473
        if (ret) {
1470
1474
                pr_info("Saving IO-APIC state failed: %d\n", ret);
1471
1475
                goto out;
1473
1477
 
1474
1478
        local_irq_save(flags);
1475
1479
        legacy_pic->mask_all();
1476
 
        mask_IO_APIC_setup(ioapic_entries);
 
1480
        mask_ioapic_entries();
1477
1481
 
1478
1482
        if (dmar_table_init_ret)
1479
1483
                ret = 0;
1504
1508
 
1505
1509
nox2apic:
1506
1510
        if (!ret) /* IR enabling failed */
1507
 
                restore_IO_APIC_setup(ioapic_entries);
 
1511
                restore_ioapic_entries();
1508
1512
        legacy_pic->restore_mask();
1509
1513
        local_irq_restore(flags);
1510
1514
 
1511
1515
out:
1512
 
        if (ioapic_entries)
1513
 
                free_ioapic_entries(ioapic_entries);
1514
 
 
1515
1516
        if (x2apic_enabled)
1516
1517
                return;
1517
1518
 
1540
1541
}
1541
1542
#else
1542
1543
 
1543
 
static int apic_verify(void)
 
1544
static int __init apic_verify(void)
1544
1545
{
1545
1546
        u32 features, h, l;
1546
1547
 
1565
1566
        return 0;
1566
1567
}
1567
1568
 
1568
 
int apic_force_enable(void)
 
1569
int __init apic_force_enable(unsigned long addr)
1569
1570
{
1570
1571
        u32 h, l;
1571
1572
 
1581
1582
        if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1582
1583
                pr_info("Local APIC disabled by BIOS -- reenabling.\n");
1583
1584
                l &= ~MSR_IA32_APICBASE_BASE;
1584
 
                l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
 
1585
                l |= MSR_IA32_APICBASE_ENABLE | addr;
1585
1586
                wrmsr(MSR_IA32_APICBASE, l, h);
1586
1587
                enabled_via_apicbase = 1;
1587
1588
        }
1622
1623
                                "you can enable it with \"lapic\"\n");
1623
1624
                        return -1;
1624
1625
                }
1625
 
                if (apic_force_enable())
 
1626
                if (apic_force_enable(APIC_DEFAULT_PHYS_BASE))
1626
1627
                        return -1;
1627
1628
        } else {
1628
1629
                if (apic_verify())
1791
1792
 
1792
1793
        exit_idle();
1793
1794
        irq_enter();
1794
 
        trace_irq_entry(SPURIOUS_APIC_VECTOR, NULL, NULL);
1795
1795
        /*
1796
1796
         * Check if this really is a spurious interrupt and ACK it
1797
1797
         * if it is a vectored one.  Just in case...
1806
1806
        /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1807
1807
        pr_info("spurious APIC interrupt on CPU#%d, "
1808
1808
                "should never happen.\n", smp_processor_id());
1809
 
        trace_irq_exit(IRQ_HANDLED);
1810
1809
        irq_exit();
1811
1810
}
1812
1811
 
1815
1814
 */
1816
1815
void smp_error_interrupt(struct pt_regs *regs)
1817
1816
{
1818
 
        u32 v, v1;
 
1817
        u32 v0, v1;
 
1818
        u32 i = 0;
 
1819
        static const char * const error_interrupt_reason[] = {
 
1820
                "Send CS error",                /* APIC Error Bit 0 */
 
1821
                "Receive CS error",             /* APIC Error Bit 1 */
 
1822
                "Send accept error",            /* APIC Error Bit 2 */
 
1823
                "Receive accept error",         /* APIC Error Bit 3 */
 
1824
                "Redirectable IPI",             /* APIC Error Bit 4 */
 
1825
                "Send illegal vector",          /* APIC Error Bit 5 */
 
1826
                "Received illegal vector",      /* APIC Error Bit 6 */
 
1827
                "Illegal register address",     /* APIC Error Bit 7 */
 
1828
        };
1819
1829
 
1820
1830
        exit_idle();
1821
1831
        irq_enter();
1822
 
        trace_irq_entry(ERROR_APIC_VECTOR, NULL, NULL);
1823
1832
        /* First tickle the hardware, only then report what went on. -- REW */
1824
 
        v = apic_read(APIC_ESR);
 
1833
        v0 = apic_read(APIC_ESR);
1825
1834
        apic_write(APIC_ESR, 0);
1826
1835
        v1 = apic_read(APIC_ESR);
1827
1836
        ack_APIC_irq();
1828
1837
        atomic_inc(&irq_err_count);
1829
1838
 
1830
 
        /*
1831
 
         * Here is what the APIC error bits mean:
1832
 
         * 0: Send CS error
1833
 
         * 1: Receive CS error
1834
 
         * 2: Send accept error
1835
 
         * 3: Receive accept error
1836
 
         * 4: Reserved
1837
 
         * 5: Send illegal vector
1838
 
         * 6: Received illegal vector
1839
 
         * 7: Illegal register address
1840
 
         */
1841
 
        pr_debug("APIC error on CPU%d: %02x(%02x)\n",
1842
 
                smp_processor_id(), v , v1);
1843
 
        trace_irq_exit(IRQ_HANDLED);
 
1839
        apic_printk(APIC_DEBUG, KERN_DEBUG "APIC error on CPU%d: %02x(%02x)",
 
1840
                    smp_processor_id(), v0 , v1);
 
1841
 
 
1842
        v1 = v1 & 0xff;
 
1843
        while (v1) {
 
1844
                if (v1 & 0x1)
 
1845
                        apic_printk(APIC_DEBUG, KERN_CONT " : %s", error_interrupt_reason[i]);
 
1846
                i++;
 
1847
                v1 >>= 1;
 
1848
        };
 
1849
 
 
1850
        apic_printk(APIC_DEBUG, KERN_CONT "\n");
 
1851
 
1844
1852
        irq_exit();
1845
1853
}
1846
1854
 
1937
1945
{
1938
1946
        int cpu;
1939
1947
 
1940
 
        /*
1941
 
         * Validate version
1942
 
         */
1943
 
        if (version == 0x0) {
1944
 
                pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
1945
 
                           "fixing up to 0x10. (tell your hw vendor)\n",
1946
 
                                version);
1947
 
                version = 0x10;
1948
 
        }
1949
 
        apic_version[apicid] = version;
1950
 
 
1951
1948
        if (num_processors >= nr_cpu_ids) {
1952
1949
                int max = nr_cpu_ids;
1953
1950
                int thiscpu = max + disabled_cpus;
1961
1958
        }
1962
1959
 
1963
1960
        num_processors++;
1964
 
        cpu = cpumask_next_zero(-1, cpu_present_mask);
1965
 
 
1966
 
        if (version != apic_version[boot_cpu_physical_apicid])
1967
 
                WARN_ONCE(1,
1968
 
                        "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n",
1969
 
                        apic_version[boot_cpu_physical_apicid], cpu, version);
1970
 
 
1971
 
        physid_set(apicid, phys_cpu_present_map);
1972
1961
        if (apicid == boot_cpu_physical_apicid) {
1973
1962
                /*
1974
1963
                 * x86_bios_cpu_apicid is required to have processors listed
1975
1964
                 * in same order as logical cpu numbers. Hence the first
1976
1965
                 * entry is BSP, and so on.
 
1966
                 * boot_cpu_init() already hold bit 0 in cpu_present_mask
 
1967
                 * for BSP.
1977
1968
                 */
1978
1969
                cpu = 0;
1979
 
        }
 
1970
        } else
 
1971
                cpu = cpumask_next_zero(-1, cpu_present_mask);
 
1972
 
 
1973
        /*
 
1974
         * Validate version
 
1975
         */
 
1976
        if (version == 0x0) {
 
1977
                pr_warning("BIOS bug: APIC version is 0 for CPU %d/0x%x, fixing up to 0x10\n",
 
1978
                           cpu, apicid);
 
1979
                version = 0x10;
 
1980
        }
 
1981
        apic_version[apicid] = version;
 
1982
 
 
1983
        if (version != apic_version[boot_cpu_physical_apicid]) {
 
1984
                pr_warning("BIOS bug: APIC version mismatch, boot CPU: %x, CPU %d: version %x\n",
 
1985
                        apic_version[boot_cpu_physical_apicid], cpu, version);
 
1986
        }
 
1987
 
 
1988
        physid_set(apicid, phys_cpu_present_map);
1980
1989
        if (apicid > max_physical_apicid)
1981
1990
                max_physical_apicid = apicid;
1982
1991
 
1984
1993
        early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1985
1994
        early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1986
1995
#endif
1987
 
 
 
1996
#ifdef CONFIG_X86_32
 
1997
        early_per_cpu(x86_cpu_to_logical_apicid, cpu) =
 
1998
                apic->x86_32_early_logical_apicid(cpu);
 
1999
#endif
1988
2000
        set_cpu_possible(cpu, true);
1989
2001
        set_cpu_present(cpu, true);
1990
2002
}
2004
2016
        apic_write(APIC_LDR, val);
2005
2017
}
2006
2018
 
2007
 
#ifdef CONFIG_X86_32
2008
 
int default_apicid_to_node(int logical_apicid)
2009
 
{
2010
 
#ifdef CONFIG_SMP
2011
 
        return apicid_2_node[hard_smp_processor_id()];
2012
 
#else
2013
 
        return 0;
2014
 
#endif
2015
 
}
2016
 
#endif
2017
 
 
2018
2019
/*
2019
2020
 * Power management
2020
2021
 */
2043
2044
        unsigned int apic_thmr;
2044
2045
} apic_pm_state;
2045
2046
 
2046
 
static int lapic_suspend(struct sys_device *dev, pm_message_t state)
 
2047
static int lapic_suspend(void)
2047
2048
{
2048
2049
        unsigned long flags;
2049
2050
        int maxlvt;
2081
2082
        return 0;
2082
2083
}
2083
2084
 
2084
 
static int lapic_resume(struct sys_device *dev)
 
2085
static void lapic_resume(void)
2085
2086
{
2086
2087
        unsigned int l, h;
2087
2088
        unsigned long flags;
2088
2089
        int maxlvt;
2089
 
        int ret = 0;
2090
 
        struct IO_APIC_route_entry **ioapic_entries = NULL;
2091
2090
 
2092
2091
        if (!apic_pm_state.active)
2093
 
                return 0;
 
2092
                return;
2094
2093
 
2095
2094
        local_irq_save(flags);
2096
2095
        if (intr_remapping_enabled) {
2097
 
                ioapic_entries = alloc_ioapic_entries();
2098
 
                if (!ioapic_entries) {
2099
 
                        WARN(1, "Alloc ioapic_entries in lapic resume failed.");
2100
 
                        ret = -ENOMEM;
2101
 
                        goto restore;
2102
 
                }
2103
 
 
2104
 
                ret = save_IO_APIC_setup(ioapic_entries);
2105
 
                if (ret) {
2106
 
                        WARN(1, "Saving IO-APIC state failed: %d\n", ret);
2107
 
                        free_ioapic_entries(ioapic_entries);
2108
 
                        goto restore;
2109
 
                }
2110
 
 
2111
 
                mask_IO_APIC_setup(ioapic_entries);
 
2096
                /*
 
2097
                 * IO-APIC and PIC have their own resume routines.
 
2098
                 * We just mask them here to make sure the interrupt
 
2099
                 * subsystem is completely quiet while we enable x2apic
 
2100
                 * and interrupt-remapping.
 
2101
                 */
 
2102
                mask_ioapic_entries();
2112
2103
                legacy_pic->mask_all();
2113
2104
        }
2114
2105
 
2151
2142
        apic_write(APIC_ESR, 0);
2152
2143
        apic_read(APIC_ESR);
2153
2144
 
2154
 
        if (intr_remapping_enabled) {
 
2145
        if (intr_remapping_enabled)
2155
2146
                reenable_intr_remapping(x2apic_mode);
2156
 
                legacy_pic->restore_mask();
2157
 
                restore_IO_APIC_setup(ioapic_entries);
2158
 
                free_ioapic_entries(ioapic_entries);
2159
 
        }
2160
 
restore:
 
2147
 
2161
2148
        local_irq_restore(flags);
2162
 
 
2163
 
        return ret;
2164
2149
}
2165
2150
 
2166
2151
/*
2168
2153
 * are needed on every CPU up until machine_halt/restart/poweroff.
2169
2154
 */
2170
2155
 
2171
 
static struct sysdev_class lapic_sysclass = {
2172
 
        .name           = "lapic",
 
2156
static struct syscore_ops lapic_syscore_ops = {
2173
2157
        .resume         = lapic_resume,
2174
2158
        .suspend        = lapic_suspend,
2175
2159
};
2176
2160
 
2177
 
static struct sys_device device_lapic = {
2178
 
        .id     = 0,
2179
 
        .cls    = &lapic_sysclass,
2180
 
};
2181
 
 
2182
2161
static void __cpuinit apic_pm_activate(void)
2183
2162
{
2184
2163
        apic_pm_state.active = 1;
2186
2165
 
2187
2166
static int __init init_lapic_sysfs(void)
2188
2167
{
2189
 
        int error;
2190
 
 
2191
 
        if (!cpu_has_apic)
2192
 
                return 0;
2193
2168
        /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
 
2169
        if (cpu_has_apic)
 
2170
                register_syscore_ops(&lapic_syscore_ops);
2194
2171
 
2195
 
        error = sysdev_class_register(&lapic_sysclass);
2196
 
        if (!error)
2197
 
                error = sysdev_register(&device_lapic);
2198
 
        return error;
 
2172
        return 0;
2199
2173
}
2200
2174
 
2201
2175
/* local apic needs to resume before other devices access its registers. */