Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Local APIC handling, local APIC timers |
| 3 | * |
| 4 | * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com> |
| 5 | * |
| 6 | * Fixes |
| 7 | * Maciej W. Rozycki : Bits for genuine 82489DX APICs; |
| 8 | * thanks to Eric Gilmore |
| 9 | * and Rolf G. Tews |
| 10 | * for testing these extensively. |
| 11 | * Maciej W. Rozycki : Various updates and fixes. |
| 12 | * Mikael Pettersson : Power Management for UP-APIC. |
| 13 | * Pavel Machek and |
| 14 | * Mikael Pettersson : PM converted to driver model. |
| 15 | */ |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/init.h> |
| 18 | |
| 19 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/delay.h> |
| 21 | #include <linux/bootmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/mc146818rtc.h> |
| 24 | #include <linux/kernel_stat.h> |
| 25 | #include <linux/sysdev.h> |
Venkatesh Pallipadi | d25bf7e | 2006-01-11 22:44:24 +0100 | [diff] [blame] | 26 | #include <linux/module.h> |
Aaron Durbin | 3992872 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 27 | #include <linux/ioport.h> |
Thomas Gleixner | ba7eda4 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 28 | #include <linux/clockchips.h> |
Thomas Gleixner | 70a2002 | 2008-01-30 13:30:18 +0100 | [diff] [blame^] | 29 | #include <linux/acpi_pmtmr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | #include <asm/atomic.h> |
| 32 | #include <asm/smp.h> |
| 33 | #include <asm/mtrr.h> |
| 34 | #include <asm/mpspec.h> |
| 35 | #include <asm/pgalloc.h> |
| 36 | #include <asm/mach_apic.h> |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 37 | #include <asm/nmi.h> |
Andi Kleen | 95833c8 | 2006-01-11 22:44:36 +0100 | [diff] [blame] | 38 | #include <asm/idle.h> |
Andi Kleen | 73dea47 | 2006-02-03 21:50:50 +0100 | [diff] [blame] | 39 | #include <asm/proto.h> |
| 40 | #include <asm/timex.h> |
john stultz | 2d0c87c | 2007-02-16 01:28:18 -0800 | [diff] [blame] | 41 | #include <asm/hpet.h> |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 42 | #include <asm/apic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | int apic_verbosity; |
Thomas Gleixner | fb79d22 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 45 | int disable_apic_timer __cpuinitdata; |
Chris Wright | bc1d99c | 2007-10-12 23:04:23 +0200 | [diff] [blame] | 46 | static int apic_calibrate_pmtmr __initdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Linus Torvalds | 2e7c283 | 2007-03-23 11:32:31 -0700 | [diff] [blame] | 48 | /* Local APIC timer works in C2? */ |
| 49 | int local_apic_timer_c2_ok; |
| 50 | EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok); |
| 51 | |
Aaron Durbin | 3992872 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 52 | static struct resource *ioapic_resources; |
| 53 | static struct resource lapic_resource = { |
| 54 | .name = "Local APIC", |
| 55 | .flags = IORESOURCE_MEM | IORESOURCE_BUSY, |
| 56 | }; |
| 57 | |
Thomas Gleixner | d03030e | 2007-10-12 23:04:06 +0200 | [diff] [blame] | 58 | static unsigned int calibration_result; |
| 59 | |
Thomas Gleixner | ba7eda4 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 60 | static int lapic_next_event(unsigned long delta, |
| 61 | struct clock_event_device *evt); |
| 62 | static void lapic_timer_setup(enum clock_event_mode mode, |
| 63 | struct clock_event_device *evt); |
| 64 | |
| 65 | static void lapic_timer_broadcast(cpumask_t mask); |
| 66 | |
| 67 | static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen); |
| 68 | |
| 69 | static struct clock_event_device lapic_clockevent = { |
| 70 | .name = "lapic", |
| 71 | .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
| 72 | | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY, |
| 73 | .shift = 32, |
| 74 | .set_mode = lapic_timer_setup, |
| 75 | .set_next_event = lapic_next_event, |
| 76 | .broadcast = lapic_timer_broadcast, |
| 77 | .rating = 100, |
| 78 | .irq = -1, |
| 79 | }; |
| 80 | static DEFINE_PER_CPU(struct clock_event_device, lapic_events); |
| 81 | |
| 82 | static int lapic_next_event(unsigned long delta, |
| 83 | struct clock_event_device *evt) |
| 84 | { |
| 85 | apic_write(APIC_TMICT, delta); |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static void lapic_timer_setup(enum clock_event_mode mode, |
| 90 | struct clock_event_device *evt) |
| 91 | { |
| 92 | unsigned long flags; |
| 93 | unsigned int v; |
| 94 | |
| 95 | /* Lapic used as dummy for broadcast ? */ |
| 96 | if (evt->features & CLOCK_EVT_FEAT_DUMMY) |
| 97 | return; |
| 98 | |
| 99 | local_irq_save(flags); |
| 100 | |
| 101 | switch (mode) { |
| 102 | case CLOCK_EVT_MODE_PERIODIC: |
| 103 | case CLOCK_EVT_MODE_ONESHOT: |
| 104 | __setup_APIC_LVTT(calibration_result, |
| 105 | mode != CLOCK_EVT_MODE_PERIODIC, 1); |
| 106 | break; |
| 107 | case CLOCK_EVT_MODE_UNUSED: |
| 108 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 109 | v = apic_read(APIC_LVTT); |
| 110 | v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR); |
| 111 | apic_write(APIC_LVTT, v); |
| 112 | break; |
| 113 | case CLOCK_EVT_MODE_RESUME: |
| 114 | /* Nothing to do here */ |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | local_irq_restore(flags); |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Local APIC timer broadcast function |
| 123 | */ |
| 124 | static void lapic_timer_broadcast(cpumask_t mask) |
| 125 | { |
| 126 | #ifdef CONFIG_SMP |
| 127 | send_IPI_mask(mask, LOCAL_TIMER_VECTOR); |
| 128 | #endif |
| 129 | } |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | static void apic_pm_activate(void); |
| 132 | |
Fernando Luis VazquezCao | 8339e9f | 2007-05-02 19:27:17 +0200 | [diff] [blame] | 133 | void apic_wait_icr_idle(void) |
| 134 | { |
| 135 | while (apic_read(APIC_ICR) & APIC_ICR_BUSY) |
| 136 | cpu_relax(); |
| 137 | } |
| 138 | |
Thomas Gleixner | 3c6bb07 | 2008-01-30 13:30:15 +0100 | [diff] [blame] | 139 | u32 safe_apic_wait_icr_idle(void) |
Fernando Luis VazquezCao | 8339e9f | 2007-05-02 19:27:17 +0200 | [diff] [blame] | 140 | { |
Thomas Gleixner | 3c6bb07 | 2008-01-30 13:30:15 +0100 | [diff] [blame] | 141 | u32 send_status; |
Fernando Luis VazquezCao | 8339e9f | 2007-05-02 19:27:17 +0200 | [diff] [blame] | 142 | int timeout; |
| 143 | |
| 144 | timeout = 0; |
| 145 | do { |
| 146 | send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY; |
| 147 | if (!send_status) |
| 148 | break; |
| 149 | udelay(100); |
| 150 | } while (timeout++ < 1000); |
| 151 | |
| 152 | return send_status; |
| 153 | } |
| 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | void enable_NMI_through_LVT0 (void * dummy) |
| 156 | { |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 157 | unsigned int v; |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 158 | |
| 159 | /* unmask and set to NMI */ |
| 160 | v = APIC_DM_NMI; |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 161 | apic_write(APIC_LVT0, v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Thomas Gleixner | 37e650c | 2008-01-30 13:30:14 +0100 | [diff] [blame] | 164 | int lapic_get_maxlvt(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 166 | unsigned int v, maxlvt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | v = apic_read(APIC_LVR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | maxlvt = GET_APIC_MAXLVT(v); |
| 170 | return maxlvt; |
| 171 | } |
| 172 | |
Andi Kleen | 3777a95 | 2006-02-03 21:51:53 +0100 | [diff] [blame] | 173 | /* |
| 174 | * 'what should we do if we get a hw irq event on an illegal vector'. |
| 175 | * each architecture has to answer this themselves. |
| 176 | */ |
| 177 | void ack_bad_irq(unsigned int irq) |
| 178 | { |
| 179 | printk("unexpected IRQ trap at vector %02x\n", irq); |
| 180 | /* |
| 181 | * Currently unexpected vectors happen only on SMP and APIC. |
| 182 | * We _must_ ack these because every local APIC has only N |
| 183 | * irq slots per priority level, and a 'hanging, unacked' IRQ |
| 184 | * holds up an irq slot - in excessive cases (when multiple |
| 185 | * unexpected vectors occur) that might lock up the APIC |
| 186 | * completely. |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 187 | * But don't ack when the APIC is disabled. -AK |
Andi Kleen | 3777a95 | 2006-02-03 21:51:53 +0100 | [diff] [blame] | 188 | */ |
| 189 | if (!disable_apic) |
| 190 | ack_APIC_irq(); |
| 191 | } |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | void clear_local_APIC(void) |
| 194 | { |
| 195 | int maxlvt; |
| 196 | unsigned int v; |
| 197 | |
Thomas Gleixner | 37e650c | 2008-01-30 13:30:14 +0100 | [diff] [blame] | 198 | maxlvt = lapic_get_maxlvt(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | /* |
Siddha, Suresh B | 704fc59 | 2006-06-26 13:59:53 +0200 | [diff] [blame] | 201 | * Masking an LVT entry can trigger a local APIC error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | * if the vector is zero. Mask LVTERR first to prevent this. |
| 203 | */ |
| 204 | if (maxlvt >= 3) { |
| 205 | v = ERROR_APIC_VECTOR; /* any non-zero vector will do */ |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 206 | apic_write(APIC_LVTERR, v | APIC_LVT_MASKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | /* |
| 209 | * Careful: we have to set masks only first to deassert |
| 210 | * any level-triggered sources. |
| 211 | */ |
| 212 | v = apic_read(APIC_LVTT); |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 213 | apic_write(APIC_LVTT, v | APIC_LVT_MASKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | v = apic_read(APIC_LVT0); |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 215 | apic_write(APIC_LVT0, v | APIC_LVT_MASKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | v = apic_read(APIC_LVT1); |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 217 | apic_write(APIC_LVT1, v | APIC_LVT_MASKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | if (maxlvt >= 4) { |
| 219 | v = apic_read(APIC_LVTPC); |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 220 | apic_write(APIC_LVTPC, v | APIC_LVT_MASKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Clean APIC state for other OSs: |
| 225 | */ |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 226 | apic_write(APIC_LVTT, APIC_LVT_MASKED); |
| 227 | apic_write(APIC_LVT0, APIC_LVT_MASKED); |
| 228 | apic_write(APIC_LVT1, APIC_LVT_MASKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | if (maxlvt >= 3) |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 230 | apic_write(APIC_LVTERR, APIC_LVT_MASKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | if (maxlvt >= 4) |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 232 | apic_write(APIC_LVTPC, APIC_LVT_MASKED); |
Andi Kleen | 5a40b7c | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 233 | apic_write(APIC_ESR, 0); |
| 234 | apic_read(APIC_ESR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Eric W. Biederman | 208fb93 | 2005-06-25 14:57:45 -0700 | [diff] [blame] | 237 | void disconnect_bsp_APIC(int virt_wire_setup) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
Andi Kleen | a8fcf1a | 2006-09-26 10:52:30 +0200 | [diff] [blame] | 239 | /* Go back to Virtual Wire compatibility mode */ |
| 240 | unsigned long value; |
| 241 | |
| 242 | /* For the spurious interrupt use vector F, and enable it */ |
| 243 | value = apic_read(APIC_SPIV); |
| 244 | value &= ~APIC_VECTOR_MASK; |
| 245 | value |= APIC_SPIV_APIC_ENABLED; |
| 246 | value |= 0xf; |
| 247 | apic_write(APIC_SPIV, value); |
| 248 | |
| 249 | if (!virt_wire_setup) { |
Chris Wright | bc1d99c | 2007-10-12 23:04:23 +0200 | [diff] [blame] | 250 | /* |
| 251 | * For LVT0 make it edge triggered, active high, |
| 252 | * external and enabled |
| 253 | */ |
Andi Kleen | a8fcf1a | 2006-09-26 10:52:30 +0200 | [diff] [blame] | 254 | value = apic_read(APIC_LVT0); |
| 255 | value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING | |
| 256 | APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR | |
| 257 | APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED ); |
| 258 | value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING; |
| 259 | value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT); |
| 260 | apic_write(APIC_LVT0, value); |
| 261 | } else { |
| 262 | /* Disable LVT0 */ |
| 263 | apic_write(APIC_LVT0, APIC_LVT_MASKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
Eric W. Biederman | 208fb93 | 2005-06-25 14:57:45 -0700 | [diff] [blame] | 265 | |
Andi Kleen | a8fcf1a | 2006-09-26 10:52:30 +0200 | [diff] [blame] | 266 | /* For LVT1 make it edge triggered, active high, nmi and enabled */ |
| 267 | value = apic_read(APIC_LVT1); |
| 268 | value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING | |
Eric W. Biederman | 208fb93 | 2005-06-25 14:57:45 -0700 | [diff] [blame] | 269 | APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR | |
| 270 | APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED); |
Andi Kleen | a8fcf1a | 2006-09-26 10:52:30 +0200 | [diff] [blame] | 271 | value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING; |
| 272 | value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI); |
| 273 | apic_write(APIC_LVT1, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void disable_local_APIC(void) |
| 277 | { |
| 278 | unsigned int value; |
| 279 | |
| 280 | clear_local_APIC(); |
| 281 | |
| 282 | /* |
| 283 | * Disable APIC (implies clearing of registers |
| 284 | * for 82489DX!). |
| 285 | */ |
| 286 | value = apic_read(APIC_SPIV); |
| 287 | value &= ~APIC_SPIV_APIC_ENABLED; |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 288 | apic_write(APIC_SPIV, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Hiroshi Shimamoto | 9b7711f | 2007-10-19 18:21:11 -0700 | [diff] [blame] | 291 | void lapic_shutdown(void) |
| 292 | { |
| 293 | unsigned long flags; |
| 294 | |
| 295 | if (!cpu_has_apic) |
| 296 | return; |
| 297 | |
| 298 | local_irq_save(flags); |
| 299 | |
| 300 | disable_local_APIC(); |
| 301 | |
| 302 | local_irq_restore(flags); |
| 303 | } |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | /* |
| 306 | * This is to verify that we're looking at a real local APIC. |
| 307 | * Check these against your board if the CPUs aren't getting |
| 308 | * started for no apparent reason. |
| 309 | */ |
| 310 | int __init verify_local_APIC(void) |
| 311 | { |
| 312 | unsigned int reg0, reg1; |
| 313 | |
| 314 | /* |
| 315 | * The version register is read-only in a real APIC. |
| 316 | */ |
| 317 | reg0 = apic_read(APIC_LVR); |
| 318 | apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0); |
| 319 | apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK); |
| 320 | reg1 = apic_read(APIC_LVR); |
| 321 | apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1); |
| 322 | |
| 323 | /* |
| 324 | * The two version reads above should print the same |
| 325 | * numbers. If the second one is different, then we |
| 326 | * poke at a non-APIC. |
| 327 | */ |
| 328 | if (reg1 != reg0) |
| 329 | return 0; |
| 330 | |
| 331 | /* |
| 332 | * Check if the version looks reasonably. |
| 333 | */ |
| 334 | reg1 = GET_APIC_VERSION(reg0); |
| 335 | if (reg1 == 0x00 || reg1 == 0xff) |
| 336 | return 0; |
Thomas Gleixner | 37e650c | 2008-01-30 13:30:14 +0100 | [diff] [blame] | 337 | reg1 = lapic_get_maxlvt(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | if (reg1 < 0x02 || reg1 == 0xff) |
| 339 | return 0; |
| 340 | |
| 341 | /* |
| 342 | * The ID register is read/write in a real APIC. |
| 343 | */ |
| 344 | reg0 = apic_read(APIC_ID); |
| 345 | apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0); |
| 346 | apic_write(APIC_ID, reg0 ^ APIC_ID_MASK); |
| 347 | reg1 = apic_read(APIC_ID); |
| 348 | apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1); |
| 349 | apic_write(APIC_ID, reg0); |
| 350 | if (reg1 != (reg0 ^ APIC_ID_MASK)) |
| 351 | return 0; |
| 352 | |
| 353 | /* |
| 354 | * The next two are just to see if we have sane values. |
| 355 | * They're only really relevant if we're in Virtual Wire |
| 356 | * compatibility mode, but most boxes are anymore. |
| 357 | */ |
| 358 | reg0 = apic_read(APIC_LVT0); |
| 359 | apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0); |
| 360 | reg1 = apic_read(APIC_LVT1); |
| 361 | apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1); |
| 362 | |
| 363 | return 1; |
| 364 | } |
| 365 | |
| 366 | void __init sync_Arb_IDs(void) |
| 367 | { |
| 368 | /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */ |
| 369 | unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR)); |
| 370 | if (ver >= 0x14) /* P4 or higher */ |
| 371 | return; |
| 372 | |
| 373 | /* |
| 374 | * Wait for idle. |
| 375 | */ |
| 376 | apic_wait_icr_idle(); |
| 377 | |
| 378 | apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n"); |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 379 | apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | | APIC_DM_INIT); |
| 381 | } |
| 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | /* |
| 384 | * An initial setup of the virtual wire mode. |
| 385 | */ |
| 386 | void __init init_bsp_APIC(void) |
| 387 | { |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 388 | unsigned int value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | /* |
| 391 | * Don't do the setup now if we have a SMP BIOS as the |
| 392 | * through-I/O-APIC virtual wire mode might be active. |
| 393 | */ |
| 394 | if (smp_found_config || !cpu_has_apic) |
| 395 | return; |
| 396 | |
| 397 | value = apic_read(APIC_LVR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
| 399 | /* |
| 400 | * Do not trust the local APIC being empty at bootup. |
| 401 | */ |
| 402 | clear_local_APIC(); |
| 403 | |
| 404 | /* |
| 405 | * Enable APIC. |
| 406 | */ |
| 407 | value = apic_read(APIC_SPIV); |
| 408 | value &= ~APIC_VECTOR_MASK; |
| 409 | value |= APIC_SPIV_APIC_ENABLED; |
| 410 | value |= APIC_SPIV_FOCUS_DISABLED; |
| 411 | value |= SPURIOUS_APIC_VECTOR; |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 412 | apic_write(APIC_SPIV, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
| 414 | /* |
| 415 | * Set up the virtual wire mode. |
| 416 | */ |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 417 | apic_write(APIC_LVT0, APIC_DM_EXTINT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | value = APIC_DM_NMI; |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 419 | apic_write(APIC_LVT1, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Ashok Raj | e6982c6 | 2005-06-25 14:54:58 -0700 | [diff] [blame] | 422 | void __cpuinit setup_local_APIC (void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 424 | unsigned int value, maxlvt; |
Vivek Goyal | da7ed9f | 2006-03-25 16:31:16 +0100 | [diff] [blame] | 425 | int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | value = apic_read(APIC_LVR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
Andi Kleen | fe7414a | 2006-09-26 10:52:30 +0200 | [diff] [blame] | 429 | BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | /* |
| 432 | * Double-check whether this APIC is really registered. |
| 433 | * This is meaningless in clustered apic mode, so we skip it. |
| 434 | */ |
| 435 | if (!apic_id_registered()) |
| 436 | BUG(); |
| 437 | |
| 438 | /* |
| 439 | * Intel recommends to set DFR, LDR and TPR before enabling |
| 440 | * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel |
| 441 | * document number 292116). So here it goes... |
| 442 | */ |
| 443 | init_apic_ldr(); |
| 444 | |
| 445 | /* |
| 446 | * Set Task Priority to 'accept all'. We never change this |
| 447 | * later on. |
| 448 | */ |
| 449 | value = apic_read(APIC_TASKPRI); |
| 450 | value &= ~APIC_TPRI_MASK; |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 451 | apic_write(APIC_TASKPRI, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
| 453 | /* |
Vivek Goyal | da7ed9f | 2006-03-25 16:31:16 +0100 | [diff] [blame] | 454 | * After a crash, we no longer service the interrupts and a pending |
| 455 | * interrupt from previous kernel might still have ISR bit set. |
| 456 | * |
| 457 | * Most probably by now CPU has serviced that pending interrupt and |
| 458 | * it might not have done the ack_APIC_irq() because it thought, |
| 459 | * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it |
| 460 | * does not clear the ISR bit and cpu thinks it has already serivced |
| 461 | * the interrupt. Hence a vector might get locked. It was noticed |
| 462 | * for timer irq (vector 0x31). Issue an extra EOI to clear ISR. |
| 463 | */ |
| 464 | for (i = APIC_ISR_NR - 1; i >= 0; i--) { |
| 465 | value = apic_read(APIC_ISR + i*0x10); |
| 466 | for (j = 31; j >= 0; j--) { |
| 467 | if (value & (1<<j)) |
| 468 | ack_APIC_irq(); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | * Now that we are all set up, enable the APIC |
| 474 | */ |
| 475 | value = apic_read(APIC_SPIV); |
| 476 | value &= ~APIC_VECTOR_MASK; |
| 477 | /* |
| 478 | * Enable APIC |
| 479 | */ |
| 480 | value |= APIC_SPIV_APIC_ENABLED; |
| 481 | |
Andi Kleen | 3f14c74 | 2006-09-26 10:52:29 +0200 | [diff] [blame] | 482 | /* We always use processor focus */ |
| 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | /* |
| 485 | * Set spurious IRQ vector |
| 486 | */ |
| 487 | value |= SPURIOUS_APIC_VECTOR; |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 488 | apic_write(APIC_SPIV, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
| 490 | /* |
| 491 | * Set up LVT0, LVT1: |
| 492 | * |
| 493 | * set up through-local-APIC on the BP's LINT0. This is not |
| 494 | * strictly necessary in pure symmetric-IO mode, but sometimes |
| 495 | * we delegate interrupts to the 8259A. |
| 496 | */ |
| 497 | /* |
| 498 | * TODO: set up through-local-APIC from through-I/O-APIC? --macro |
| 499 | */ |
| 500 | value = apic_read(APIC_LVT0) & APIC_LVT_MASKED; |
Andi Kleen | a8fcf1a | 2006-09-26 10:52:30 +0200 | [diff] [blame] | 501 | if (!smp_processor_id() && !value) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | value = APIC_DM_EXTINT; |
Chris Wright | bc1d99c | 2007-10-12 23:04:23 +0200 | [diff] [blame] | 503 | apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", |
| 504 | smp_processor_id()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } else { |
| 506 | value = APIC_DM_EXTINT | APIC_LVT_MASKED; |
Chris Wright | bc1d99c | 2007-10-12 23:04:23 +0200 | [diff] [blame] | 507 | apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", |
| 508 | smp_processor_id()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 510 | apic_write(APIC_LVT0, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
| 512 | /* |
| 513 | * only the BP should see the LINT1 NMI signal, obviously. |
| 514 | */ |
| 515 | if (!smp_processor_id()) |
| 516 | value = APIC_DM_NMI; |
| 517 | else |
| 518 | value = APIC_DM_NMI | APIC_LVT_MASKED; |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 519 | apic_write(APIC_LVT1, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Andi Kleen | 61c1134 | 2005-09-12 18:49:23 +0200 | [diff] [blame] | 521 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | unsigned oldvalue; |
Thomas Gleixner | 37e650c | 2008-01-30 13:30:14 +0100 | [diff] [blame] | 523 | maxlvt = lapic_get_maxlvt(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | oldvalue = apic_read(APIC_ESR); |
| 525 | value = ERROR_APIC_VECTOR; // enables sending errors |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 526 | apic_write(APIC_LVTERR, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | /* |
| 528 | * spec says clear errors after enabling vector. |
| 529 | */ |
| 530 | if (maxlvt > 3) |
| 531 | apic_write(APIC_ESR, 0); |
| 532 | value = apic_read(APIC_ESR); |
| 533 | if (value != oldvalue) |
| 534 | apic_printk(APIC_VERBOSE, |
| 535 | "ESR value after enabling vector: %08x, after %08x\n", |
| 536 | oldvalue, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | nmi_watchdog_default(); |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 540 | setup_apic_nmi_watchdog(NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | apic_pm_activate(); |
| 542 | } |
| 543 | |
| 544 | #ifdef CONFIG_PM |
| 545 | |
| 546 | static struct { |
| 547 | /* 'active' is true if the local APIC was enabled by us and |
| 548 | not the BIOS; this signifies that we are also responsible |
| 549 | for disabling it before entering apm/acpi suspend */ |
| 550 | int active; |
| 551 | /* r/w apic fields */ |
| 552 | unsigned int apic_id; |
| 553 | unsigned int apic_taskpri; |
| 554 | unsigned int apic_ldr; |
| 555 | unsigned int apic_dfr; |
| 556 | unsigned int apic_spiv; |
| 557 | unsigned int apic_lvtt; |
| 558 | unsigned int apic_lvtpc; |
| 559 | unsigned int apic_lvt0; |
| 560 | unsigned int apic_lvt1; |
| 561 | unsigned int apic_lvterr; |
| 562 | unsigned int apic_tmict; |
| 563 | unsigned int apic_tdcr; |
| 564 | unsigned int apic_thmr; |
| 565 | } apic_pm_state; |
| 566 | |
Pavel Machek | 0b9c33a | 2005-04-16 15:25:31 -0700 | [diff] [blame] | 567 | static int lapic_suspend(struct sys_device *dev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | { |
| 569 | unsigned long flags; |
Karsten Wiese | f990fff | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 570 | int maxlvt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
| 572 | if (!apic_pm_state.active) |
| 573 | return 0; |
| 574 | |
Thomas Gleixner | 37e650c | 2008-01-30 13:30:14 +0100 | [diff] [blame] | 575 | maxlvt = lapic_get_maxlvt(); |
Karsten Wiese | f990fff | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | apic_pm_state.apic_id = apic_read(APIC_ID); |
| 578 | apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI); |
| 579 | apic_pm_state.apic_ldr = apic_read(APIC_LDR); |
| 580 | apic_pm_state.apic_dfr = apic_read(APIC_DFR); |
| 581 | apic_pm_state.apic_spiv = apic_read(APIC_SPIV); |
| 582 | apic_pm_state.apic_lvtt = apic_read(APIC_LVTT); |
Karsten Wiese | f990fff | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 583 | if (maxlvt >= 4) |
| 584 | apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0); |
| 586 | apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1); |
| 587 | apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR); |
| 588 | apic_pm_state.apic_tmict = apic_read(APIC_TMICT); |
| 589 | apic_pm_state.apic_tdcr = apic_read(APIC_TDCR); |
Karsten Wiese | f990fff | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 590 | #ifdef CONFIG_X86_MCE_INTEL |
| 591 | if (maxlvt >= 5) |
| 592 | apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR); |
| 593 | #endif |
Fernando Luis Vázquez Cao | 2b94ab2 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 594 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | disable_local_APIC(); |
| 596 | local_irq_restore(flags); |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static int lapic_resume(struct sys_device *dev) |
| 601 | { |
| 602 | unsigned int l, h; |
| 603 | unsigned long flags; |
Karsten Wiese | f990fff | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 604 | int maxlvt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
| 606 | if (!apic_pm_state.active) |
| 607 | return 0; |
| 608 | |
Thomas Gleixner | 37e650c | 2008-01-30 13:30:14 +0100 | [diff] [blame] | 609 | maxlvt = lapic_get_maxlvt(); |
Karsten Wiese | f990fff | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | local_irq_save(flags); |
| 612 | rdmsr(MSR_IA32_APICBASE, l, h); |
| 613 | l &= ~MSR_IA32_APICBASE_BASE; |
Shaohua Li | 5b74357 | 2006-01-16 01:56:45 +0100 | [diff] [blame] | 614 | l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | wrmsr(MSR_IA32_APICBASE, l, h); |
| 616 | apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED); |
| 617 | apic_write(APIC_ID, apic_pm_state.apic_id); |
| 618 | apic_write(APIC_DFR, apic_pm_state.apic_dfr); |
| 619 | apic_write(APIC_LDR, apic_pm_state.apic_ldr); |
| 620 | apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri); |
| 621 | apic_write(APIC_SPIV, apic_pm_state.apic_spiv); |
| 622 | apic_write(APIC_LVT0, apic_pm_state.apic_lvt0); |
| 623 | apic_write(APIC_LVT1, apic_pm_state.apic_lvt1); |
Karsten Wiese | f990fff | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 624 | #ifdef CONFIG_X86_MCE_INTEL |
| 625 | if (maxlvt >= 5) |
| 626 | apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr); |
| 627 | #endif |
| 628 | if (maxlvt >= 4) |
| 629 | apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | apic_write(APIC_LVTT, apic_pm_state.apic_lvtt); |
| 631 | apic_write(APIC_TDCR, apic_pm_state.apic_tdcr); |
| 632 | apic_write(APIC_TMICT, apic_pm_state.apic_tmict); |
| 633 | apic_write(APIC_ESR, 0); |
| 634 | apic_read(APIC_ESR); |
| 635 | apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr); |
| 636 | apic_write(APIC_ESR, 0); |
| 637 | apic_read(APIC_ESR); |
| 638 | local_irq_restore(flags); |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | static struct sysdev_class lapic_sysclass = { |
Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 643 | .name = "lapic", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | .resume = lapic_resume, |
| 645 | .suspend = lapic_suspend, |
| 646 | }; |
| 647 | |
| 648 | static struct sys_device device_lapic = { |
| 649 | .id = 0, |
| 650 | .cls = &lapic_sysclass, |
| 651 | }; |
| 652 | |
Ashok Raj | e6982c6 | 2005-06-25 14:54:58 -0700 | [diff] [blame] | 653 | static void __cpuinit apic_pm_activate(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | { |
| 655 | apic_pm_state.active = 1; |
| 656 | } |
| 657 | |
| 658 | static int __init init_lapic_sysfs(void) |
| 659 | { |
| 660 | int error; |
| 661 | if (!cpu_has_apic) |
| 662 | return 0; |
| 663 | /* XXX: remove suspend/resume procs if !apic_pm_state.active? */ |
| 664 | error = sysdev_class_register(&lapic_sysclass); |
| 665 | if (!error) |
| 666 | error = sysdev_register(&device_lapic); |
| 667 | return error; |
| 668 | } |
| 669 | device_initcall(init_lapic_sysfs); |
| 670 | |
| 671 | #else /* CONFIG_PM */ |
| 672 | |
| 673 | static void apic_pm_activate(void) { } |
| 674 | |
| 675 | #endif /* CONFIG_PM */ |
| 676 | |
| 677 | static int __init apic_set_verbosity(char *str) |
| 678 | { |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 679 | if (str == NULL) { |
| 680 | skip_ioapic_setup = 0; |
| 681 | ioapic_force = 1; |
| 682 | return 0; |
| 683 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | if (strcmp("debug", str) == 0) |
| 685 | apic_verbosity = APIC_DEBUG; |
| 686 | else if (strcmp("verbose", str) == 0) |
| 687 | apic_verbosity = APIC_VERBOSE; |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 688 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | printk(KERN_WARNING "APIC Verbosity level %s not recognised" |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 690 | " use apic=verbose or apic=debug\n", str); |
| 691 | return -EINVAL; |
| 692 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 694 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 696 | early_param("apic", apic_set_verbosity); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
| 698 | /* |
| 699 | * Detect and enable local APICs on non-SMP boards. |
| 700 | * Original code written by Keir Fraser. |
| 701 | * On AMD64 we trust the BIOS - if it says no APIC it is likely |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 702 | * not correctly set up (usually the APIC timer won't work etc.) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | */ |
| 704 | |
| 705 | static int __init detect_init_APIC (void) |
| 706 | { |
| 707 | if (!cpu_has_apic) { |
| 708 | printk(KERN_INFO "No local APIC present\n"); |
| 709 | return -1; |
| 710 | } |
| 711 | |
| 712 | mp_lapic_addr = APIC_DEFAULT_PHYS_BASE; |
| 713 | boot_cpu_id = 0; |
| 714 | return 0; |
| 715 | } |
| 716 | |
Aaron Durbin | 3992872 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 717 | #ifdef CONFIG_X86_IO_APIC |
| 718 | static struct resource * __init ioapic_setup_resources(void) |
| 719 | { |
| 720 | #define IOAPIC_RESOURCE_NAME_SIZE 11 |
| 721 | unsigned long n; |
| 722 | struct resource *res; |
| 723 | char *mem; |
| 724 | int i; |
| 725 | |
| 726 | if (nr_ioapics <= 0) |
| 727 | return NULL; |
| 728 | |
| 729 | n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource); |
| 730 | n *= nr_ioapics; |
| 731 | |
| 732 | mem = alloc_bootmem(n); |
| 733 | res = (void *)mem; |
| 734 | |
| 735 | if (mem != NULL) { |
| 736 | memset(mem, 0, n); |
| 737 | mem += sizeof(struct resource) * nr_ioapics; |
| 738 | |
| 739 | for (i = 0; i < nr_ioapics; i++) { |
| 740 | res[i].name = mem; |
| 741 | res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
| 742 | sprintf(mem, "IOAPIC %u", i); |
| 743 | mem += IOAPIC_RESOURCE_NAME_SIZE; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | ioapic_resources = res; |
| 748 | |
| 749 | return res; |
| 750 | } |
| 751 | |
| 752 | static int __init ioapic_insert_resources(void) |
| 753 | { |
| 754 | int i; |
| 755 | struct resource *r = ioapic_resources; |
| 756 | |
| 757 | if (!r) { |
| 758 | printk("IO APIC resources could be not be allocated.\n"); |
| 759 | return -1; |
| 760 | } |
| 761 | |
| 762 | for (i = 0; i < nr_ioapics; i++) { |
| 763 | insert_resource(&iomem_resource, r); |
| 764 | r++; |
| 765 | } |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | /* Insert the IO APIC resources after PCI initialization has occured to handle |
| 771 | * IO APICS that are mapped in on a BAR in PCI space. */ |
| 772 | late_initcall(ioapic_insert_resources); |
| 773 | #endif |
| 774 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | void __init init_apic_mappings(void) |
| 776 | { |
| 777 | unsigned long apic_phys; |
| 778 | |
| 779 | /* |
| 780 | * If no local APIC can be found then set up a fake all |
| 781 | * zeroes page to simulate the local APIC and another |
| 782 | * one for the IO-APIC. |
| 783 | */ |
| 784 | if (!smp_found_config && detect_init_APIC()) { |
| 785 | apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE); |
| 786 | apic_phys = __pa(apic_phys); |
| 787 | } else |
| 788 | apic_phys = mp_lapic_addr; |
| 789 | |
| 790 | set_fixmap_nocache(FIX_APIC_BASE, apic_phys); |
Yinghai Lu | 7ffeeb1 | 2007-10-12 23:04:06 +0200 | [diff] [blame] | 791 | apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n", |
| 792 | APIC_BASE, apic_phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
Aaron Durbin | 3992872 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 794 | /* Put local APIC into the resource map. */ |
| 795 | lapic_resource.start = apic_phys; |
| 796 | lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1; |
| 797 | insert_resource(&iomem_resource, &lapic_resource); |
| 798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | /* |
| 800 | * Fetch the APIC ID of the BSP in case we have a |
| 801 | * default configuration (or the MP table is broken). |
| 802 | */ |
Andi Kleen | 1d3fbbf | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 803 | boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | { |
| 806 | unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0; |
| 807 | int i; |
Aaron Durbin | 3992872 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 808 | struct resource *ioapic_res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
Aaron Durbin | 3992872 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 810 | ioapic_res = ioapic_setup_resources(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | for (i = 0; i < nr_ioapics; i++) { |
| 812 | if (smp_found_config) { |
| 813 | ioapic_phys = mp_ioapics[i].mpc_apicaddr; |
| 814 | } else { |
Chris Wright | bc1d99c | 2007-10-12 23:04:23 +0200 | [diff] [blame] | 815 | ioapic_phys = (unsigned long) |
| 816 | alloc_bootmem_pages(PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | ioapic_phys = __pa(ioapic_phys); |
| 818 | } |
| 819 | set_fixmap_nocache(idx, ioapic_phys); |
Chris Wright | bc1d99c | 2007-10-12 23:04:23 +0200 | [diff] [blame] | 820 | apic_printk(APIC_VERBOSE, |
| 821 | "mapped IOAPIC to %016lx (%016lx)\n", |
| 822 | __fix_to_virt(idx), ioapic_phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | idx++; |
Aaron Durbin | 3992872 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 824 | |
| 825 | if (ioapic_res != NULL) { |
| 826 | ioapic_res->start = ioapic_phys; |
| 827 | ioapic_res->end = ioapic_phys + (4 * 1024) - 1; |
| 828 | ioapic_res++; |
| 829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | } |
| 831 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /* |
| 835 | * This function sets up the local APIC timer, with a timeout of |
| 836 | * 'clocks' APIC bus clock. During calibration we actually call |
| 837 | * this function twice on the boot CPU, once with a bogus timeout |
| 838 | * value, second time for real. The other (noncalibrating) CPUs |
| 839 | * call this function only once, with the real, calibrated value. |
| 840 | * |
| 841 | * We do reads before writes even if unnecessary, to get around the |
| 842 | * P5 APIC double write bug. |
| 843 | */ |
| 844 | |
Thomas Gleixner | 8017409 | 2007-10-12 23:04:06 +0200 | [diff] [blame] | 845 | static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | { |
David Rientjes | 86bd58b | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 847 | unsigned int lvtt_value, tmp_value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
Thomas Gleixner | 8017409 | 2007-10-12 23:04:06 +0200 | [diff] [blame] | 849 | lvtt_value = LOCAL_TIMER_VECTOR; |
| 850 | if (!oneshot) |
| 851 | lvtt_value |= APIC_LVT_TIMER_PERIODIC; |
| 852 | if (!irqen) |
Venkatesh Pallipadi | d25bf7e | 2006-01-11 22:44:24 +0100 | [diff] [blame] | 853 | lvtt_value |= APIC_LVT_MASKED; |
| 854 | |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 855 | apic_write(APIC_LVTT, lvtt_value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
| 857 | /* |
| 858 | * Divide PICLK by 16 |
| 859 | */ |
| 860 | tmp_value = apic_read(APIC_TDCR); |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 861 | apic_write(APIC_TDCR, (tmp_value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
| 863 | | APIC_TDR_DIV_16); |
| 864 | |
Thomas Gleixner | 8017409 | 2007-10-12 23:04:06 +0200 | [diff] [blame] | 865 | if (!oneshot) |
Thomas Gleixner | b58eb00 | 2007-10-12 23:04:06 +0200 | [diff] [blame] | 866 | apic_write(APIC_TMICT, clocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Thomas Gleixner | abc63fc | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 869 | static void setup_APIC_timer(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | { |
Thomas Gleixner | b8ce335 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 871 | struct clock_event_device *levt = &__get_cpu_var(lapic_events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | |
Thomas Gleixner | b8ce335 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 873 | memcpy(levt, &lapic_clockevent, sizeof(*levt)); |
| 874 | levt->cpumask = cpumask_of_cpu(smp_processor_id()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
Thomas Gleixner | b8ce335 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 876 | clockevents_register_device(levt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | /* |
| 880 | * In this function we calibrate APIC bus clocks to the external |
| 881 | * timer. Unfortunately we cannot use jiffies and the timer irq |
| 882 | * to calibrate, since some later bootup code depends on getting |
| 883 | * the first irq? Ugh. |
| 884 | * |
| 885 | * We want to do the calibration only once since we |
| 886 | * want to have local timer irqs syncron. CPUs connected |
| 887 | * by the same APIC bus have the very same bus frequency. |
| 888 | * And we want to have irqs off anyways, no accidental |
| 889 | * APIC irq that way. |
| 890 | */ |
| 891 | |
| 892 | #define TICK_COUNT 100000000 |
| 893 | |
Thomas Gleixner | d03030e | 2007-10-12 23:04:06 +0200 | [diff] [blame] | 894 | static void __init calibrate_APIC_clock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | { |
David P. Reed | 4637a74 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 896 | unsigned apic, apic_start; |
| 897 | unsigned long tsc, tsc_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | int result; |
Thomas Gleixner | c4d58cb | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 899 | |
| 900 | local_irq_disable(); |
| 901 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | /* |
| 903 | * Put whatever arbitrary (but long enough) timeout |
| 904 | * value into the APIC clock, we just want to get the |
| 905 | * counter running for calibration. |
Thomas Gleixner | 8017409 | 2007-10-12 23:04:06 +0200 | [diff] [blame] | 906 | * |
| 907 | * No interrupt enable ! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | */ |
Thomas Gleixner | b58eb00 | 2007-10-12 23:04:06 +0200 | [diff] [blame] | 909 | __setup_APIC_LVTT(250000000, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
| 911 | apic_start = apic_read(APIC_TMCCT); |
Andi Kleen | 0c3749c | 2006-02-03 21:51:41 +0100 | [diff] [blame] | 912 | #ifdef CONFIG_X86_PM_TIMER |
| 913 | if (apic_calibrate_pmtmr && pmtmr_ioport) { |
| 914 | pmtimer_wait(5000); /* 5ms wait */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | apic = apic_read(APIC_TMCCT); |
Andi Kleen | 0c3749c | 2006-02-03 21:51:41 +0100 | [diff] [blame] | 916 | result = (apic_start - apic) * 1000L / 5; |
| 917 | } else |
| 918 | #endif |
| 919 | { |
David P. Reed | 4637a74 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 920 | rdtscll(tsc_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | |
Andi Kleen | 0c3749c | 2006-02-03 21:51:41 +0100 | [diff] [blame] | 922 | do { |
| 923 | apic = apic_read(APIC_TMCCT); |
David P. Reed | 4637a74 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 924 | rdtscll(tsc); |
Andi Kleen | 0c3749c | 2006-02-03 21:51:41 +0100 | [diff] [blame] | 925 | } while ((tsc - tsc_start) < TICK_COUNT && |
David P. Reed | 4637a74 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 926 | (apic_start - apic) < TICK_COUNT); |
Andi Kleen | 0c3749c | 2006-02-03 21:51:41 +0100 | [diff] [blame] | 927 | |
Joerg Roedel | 6b37f5a | 2007-05-02 19:27:06 +0200 | [diff] [blame] | 928 | result = (apic_start - apic) * 1000L * tsc_khz / |
Andi Kleen | 0c3749c | 2006-02-03 21:51:41 +0100 | [diff] [blame] | 929 | (tsc - tsc_start); |
| 930 | } |
Thomas Gleixner | c4d58cb | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 931 | |
| 932 | local_irq_enable(); |
| 933 | |
Thomas Gleixner | d03030e | 2007-10-12 23:04:06 +0200 | [diff] [blame] | 934 | printk(KERN_DEBUG "APIC timer calibration result %d\n", result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | |
| 936 | printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n", |
| 937 | result / 1000 / 1000, result / 1000 % 1000); |
| 938 | |
Thomas Gleixner | ba7eda4 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 939 | /* Calculate the scaled math multiplication factor */ |
| 940 | lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC, 32); |
| 941 | lapic_clockevent.max_delta_ns = |
| 942 | clockevent_delta2ns(0x7FFFFF, &lapic_clockevent); |
| 943 | lapic_clockevent.min_delta_ns = |
| 944 | clockevent_delta2ns(0xF, &lapic_clockevent); |
| 945 | |
Thomas Gleixner | b58eb00 | 2007-10-12 23:04:06 +0200 | [diff] [blame] | 946 | calibration_result = result / HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | void __init setup_boot_APIC_clock (void) |
| 950 | { |
Thomas Gleixner | b8ce335 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 951 | /* |
| 952 | * The local apic timer can be disabled via the kernel commandline. |
| 953 | * Register the lapic timer as a dummy clock event source on SMP |
| 954 | * systems, so the broadcast mechanism is used. On UP systems simply |
| 955 | * ignore it. |
| 956 | */ |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 957 | if (disable_apic_timer) { |
| 958 | printk(KERN_INFO "Disabling APIC timer\n"); |
Thomas Gleixner | b8ce335 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 959 | /* No broadcast on UP ! */ |
| 960 | if (num_possible_cpus() > 1) |
| 961 | setup_APIC_timer(); |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 962 | return; |
| 963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
| 965 | printk(KERN_INFO "Using local APIC timer interrupts.\n"); |
Thomas Gleixner | d03030e | 2007-10-12 23:04:06 +0200 | [diff] [blame] | 966 | calibrate_APIC_clock(); |
Thomas Gleixner | b8ce335 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | /* |
Thomas Gleixner | b8ce335 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 969 | * If nmi_watchdog is set to IO_APIC, we need the |
| 970 | * PIT/HPET going. Otherwise register lapic as a dummy |
| 971 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | */ |
Thomas Gleixner | b8ce335 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 973 | if (nmi_watchdog != NMI_IO_APIC) |
| 974 | lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY; |
| 975 | else |
| 976 | printk(KERN_WARNING "APIC timer registered as dummy," |
| 977 | " due to nmi_watchdog=1!\n"); |
| 978 | |
Thomas Gleixner | abc63fc | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 979 | setup_APIC_timer(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Thomas Gleixner | 89039b3 | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 982 | /* |
| 983 | * AMD C1E enabled CPUs have a real nasty problem: Some BIOSes set the |
| 984 | * C1E flag only in the secondary CPU, so when we detect the wreckage |
| 985 | * we already have enabled the boot CPU local apic timer. Check, if |
| 986 | * disable_apic_timer is set and the DUMMY flag is cleared. If yes, |
| 987 | * set the DUMMY flag again and force the broadcast mode in the |
| 988 | * clockevents layer. |
| 989 | */ |
| 990 | void __cpuinit check_boot_apic_timer_broadcast(void) |
| 991 | { |
Thomas Gleixner | 89039b3 | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 992 | if (!disable_apic_timer || |
| 993 | (lapic_clockevent.features & CLOCK_EVT_FEAT_DUMMY)) |
| 994 | return; |
| 995 | |
| 996 | printk(KERN_INFO "AMD C1E detected late. Force timer broadcast.\n"); |
| 997 | lapic_clockevent.features |= CLOCK_EVT_FEAT_DUMMY; |
Thomas Gleixner | 89039b3 | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 998 | |
| 999 | local_irq_enable(); |
| 1000 | clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE, &boot_cpu_id); |
| 1001 | local_irq_disable(); |
| 1002 | } |
| 1003 | |
Ashok Raj | e6982c6 | 2005-06-25 14:54:58 -0700 | [diff] [blame] | 1004 | void __cpuinit setup_secondary_APIC_clock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | { |
Thomas Gleixner | 89039b3 | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 1006 | check_boot_apic_timer_broadcast(); |
Thomas Gleixner | abc63fc | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 1007 | setup_APIC_timer(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | int setup_profiling_timer(unsigned int multiplier) |
| 1011 | { |
Venkatesh Pallipadi | 5a07a30 | 2006-01-11 22:44:18 +0100 | [diff] [blame] | 1012 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Thomas Gleixner | f40f31b | 2007-07-21 17:10:14 +0200 | [diff] [blame] | 1015 | void setup_APIC_extended_lvt(unsigned char lvt_off, unsigned char vector, |
| 1016 | unsigned char msg_type, unsigned char mask) |
Jacob Shin | 89b831e | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 1017 | { |
Jacob Shin | 17fc14f | 2006-06-26 13:58:47 +0200 | [diff] [blame] | 1018 | unsigned long reg = (lvt_off << 4) + K8_APIC_EXT_LVT_BASE; |
| 1019 | unsigned int v = (mask << 16) | (msg_type << 8) | vector; |
Jacob Shin | 89b831e | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 1020 | apic_write(reg, v); |
| 1021 | } |
Jacob Shin | 89b831e | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 1022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | /* |
| 1024 | * Local timer interrupt handler. It does both profiling and |
| 1025 | * process statistics/rescheduling. |
| 1026 | * |
| 1027 | * We do profiling in every local tick, statistics/rescheduling |
| 1028 | * happen only every 'profiling multiplier' ticks. The default |
| 1029 | * multiplier is 1 and it can be changed by writing the new multiplier |
| 1030 | * value into /proc/profile. |
| 1031 | */ |
| 1032 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1033 | void smp_local_timer_interrupt(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | { |
Thomas Gleixner | b8ce335 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 1035 | int cpu = smp_processor_id(); |
| 1036 | struct clock_event_device *evt = &per_cpu(lapic_events, cpu); |
| 1037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | /* |
Thomas Gleixner | b8ce335 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 1039 | * Normally we should not be here till LAPIC has been initialized but |
| 1040 | * in some cases like kdump, its possible that there is a pending LAPIC |
| 1041 | * timer interrupt from previous kernel's context and is delivered in |
| 1042 | * new kernel the moment interrupts are enabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | * |
Thomas Gleixner | b8ce335 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 1044 | * Interrupts are enabled early and LAPIC is setup much later, hence |
| 1045 | * its possible that when we get here evt->event_handler is NULL. |
| 1046 | * Check for event_handler being NULL and discard the interrupt as |
| 1047 | * spurious. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | */ |
Thomas Gleixner | b8ce335 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 1049 | if (!evt->event_handler) { |
| 1050 | printk(KERN_WARNING |
| 1051 | "Spurious LAPIC timer interrupt on cpu %d\n", cpu); |
| 1052 | /* Switch it off */ |
| 1053 | lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt); |
| 1054 | return; |
| 1055 | } |
| 1056 | |
| 1057 | /* |
| 1058 | * the NMI deadlock-detector uses this. |
| 1059 | */ |
| 1060 | add_pda(apic_timer_irqs, 1); |
| 1061 | |
| 1062 | evt->event_handler(evt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /* |
| 1066 | * Local APIC timer interrupt. This is the most natural way for doing |
| 1067 | * local interrupts, but local timer interrupts can be emulated by |
| 1068 | * broadcast interrupts too. [in case the hw doesn't support APIC timers] |
| 1069 | * |
| 1070 | * [ if a single-CPU system runs an SMP kernel then we call the local |
| 1071 | * interrupt as well. Thus we cannot inline the local irq ... ] |
| 1072 | */ |
Andrew Morton | d150ad7 | 2006-10-06 13:28:09 -0700 | [diff] [blame] | 1073 | void smp_apic_timer_interrupt(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | { |
Andrew Morton | d150ad7 | 2006-10-06 13:28:09 -0700 | [diff] [blame] | 1075 | struct pt_regs *old_regs = set_irq_regs(regs); |
| 1076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | * NOTE! We'd better ACK the irq immediately, |
| 1079 | * because timer handling can be slow. |
| 1080 | */ |
| 1081 | ack_APIC_irq(); |
| 1082 | /* |
| 1083 | * update_process_times() expects us to have done irq_enter(). |
| 1084 | * Besides, if we don't timer interrupts ignore the global |
| 1085 | * interrupt lock, which is the WrongThing (tm) to do. |
| 1086 | */ |
Andi Kleen | 95833c8 | 2006-01-11 22:44:36 +0100 | [diff] [blame] | 1087 | exit_idle(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | irq_enter(); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1089 | smp_local_timer_interrupt(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | irq_exit(); |
Andrew Morton | d150ad7 | 2006-10-06 13:28:09 -0700 | [diff] [blame] | 1091 | set_irq_regs(old_regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /* |
Vojtech Pavlik | f8bf3c6 | 2006-06-26 13:58:23 +0200 | [diff] [blame] | 1095 | * apic_is_clustered_box() -- Check if we can expect good TSC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | * |
| 1097 | * Thus far, the major user of this is IBM's Summit2 series: |
| 1098 | * |
Linus Torvalds | 637029c | 2006-02-27 20:41:56 -0800 | [diff] [blame] | 1099 | * Clustered boxes may have unsynced TSC problems if they are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | * multi-chassis. Use available data to take a good guess. |
| 1101 | * If in doubt, go HPET. |
| 1102 | */ |
Vojtech Pavlik | f8bf3c6 | 2006-06-26 13:58:23 +0200 | [diff] [blame] | 1103 | __cpuinit int apic_is_clustered_box(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | { |
| 1105 | int i, clusters, zeros; |
| 1106 | unsigned id; |
| 1107 | DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS); |
| 1108 | |
Suresh Siddha | 376ec33 | 2005-05-16 21:53:32 -0700 | [diff] [blame] | 1109 | bitmap_zero(clustermap, NUM_APIC_CLUSTERS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | |
| 1111 | for (i = 0; i < NR_CPUS; i++) { |
| 1112 | id = bios_cpu_apicid[i]; |
| 1113 | if (id != BAD_APICID) |
| 1114 | __set_bit(APIC_CLUSTERID(id), clustermap); |
| 1115 | } |
| 1116 | |
| 1117 | /* Problem: Partially populated chassis may not have CPUs in some of |
| 1118 | * the APIC clusters they have been allocated. Only present CPUs have |
| 1119 | * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since |
| 1120 | * clusters are allocated sequentially, count zeros only if they are |
| 1121 | * bounded by ones. |
| 1122 | */ |
| 1123 | clusters = 0; |
| 1124 | zeros = 0; |
| 1125 | for (i = 0; i < NUM_APIC_CLUSTERS; i++) { |
| 1126 | if (test_bit(i, clustermap)) { |
| 1127 | clusters += 1 + zeros; |
| 1128 | zeros = 0; |
| 1129 | } else |
| 1130 | ++zeros; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
Vojtech Pavlik | f8bf3c6 | 2006-06-26 13:58:23 +0200 | [diff] [blame] | 1134 | * If clusters > 2, then should be multi-chassis. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | * May have to revisit this when multi-core + hyperthreaded CPUs come |
| 1136 | * out, but AFAIK this will work even for them. |
| 1137 | */ |
| 1138 | return (clusters > 2); |
| 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * This interrupt should _never_ happen with our APIC/SMP architecture |
| 1143 | */ |
| 1144 | asmlinkage void smp_spurious_interrupt(void) |
| 1145 | { |
| 1146 | unsigned int v; |
Andi Kleen | 95833c8 | 2006-01-11 22:44:36 +0100 | [diff] [blame] | 1147 | exit_idle(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | irq_enter(); |
| 1149 | /* |
| 1150 | * Check if this really is a spurious interrupt and ACK it |
| 1151 | * if it is a vectored one. Just in case... |
| 1152 | * Spurious interrupts should not be ACKed. |
| 1153 | */ |
| 1154 | v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1)); |
| 1155 | if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f))) |
| 1156 | ack_APIC_irq(); |
| 1157 | |
Joe Korty | 38e760a | 2007-10-17 18:04:40 +0200 | [diff] [blame] | 1158 | add_pda(irq_spurious_count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | irq_exit(); |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * This interrupt should never happen with our APIC/SMP architecture |
| 1164 | */ |
| 1165 | |
| 1166 | asmlinkage void smp_error_interrupt(void) |
| 1167 | { |
| 1168 | unsigned int v, v1; |
| 1169 | |
Andi Kleen | 95833c8 | 2006-01-11 22:44:36 +0100 | [diff] [blame] | 1170 | exit_idle(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | irq_enter(); |
| 1172 | /* First tickle the hardware, only then report what went on. -- REW */ |
| 1173 | v = apic_read(APIC_ESR); |
| 1174 | apic_write(APIC_ESR, 0); |
| 1175 | v1 = apic_read(APIC_ESR); |
| 1176 | ack_APIC_irq(); |
| 1177 | atomic_inc(&irq_err_count); |
| 1178 | |
| 1179 | /* Here is what the APIC error bits mean: |
| 1180 | 0: Send CS error |
| 1181 | 1: Receive CS error |
| 1182 | 2: Send accept error |
| 1183 | 3: Receive accept error |
| 1184 | 4: Reserved |
| 1185 | 5: Send illegal vector |
| 1186 | 6: Received illegal vector |
| 1187 | 7: Illegal register address |
| 1188 | */ |
| 1189 | printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n", |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 1190 | smp_processor_id(), v , v1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | irq_exit(); |
| 1192 | } |
| 1193 | |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 1194 | int disable_apic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | |
| 1196 | /* |
| 1197 | * This initializes the IO-APIC and APIC hardware if this is |
| 1198 | * a UP kernel. |
| 1199 | */ |
| 1200 | int __init APIC_init_uniprocessor (void) |
| 1201 | { |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 1202 | if (disable_apic) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | printk(KERN_INFO "Apic disabled\n"); |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 1204 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | } |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 1206 | if (!cpu_has_apic) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | disable_apic = 1; |
| 1208 | printk(KERN_INFO "Apic disabled by BIOS\n"); |
| 1209 | return -1; |
| 1210 | } |
| 1211 | |
| 1212 | verify_local_APIC(); |
| 1213 | |
Andi Kleen | 357e11d | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 1214 | phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id); |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 1215 | apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | |
| 1217 | setup_local_APIC(); |
| 1218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | if (smp_found_config && !skip_ioapic_setup && nr_ioapics) |
Andi Kleen | 7f11d8a | 2006-09-26 10:52:29 +0200 | [diff] [blame] | 1220 | setup_IO_APIC(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | else |
| 1222 | nr_ioapics = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | setup_boot_APIC_clock(); |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 1224 | check_nmi_watchdog(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | return 0; |
| 1226 | } |
| 1227 | |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 1228 | static __init int setup_disableapic(char *str) |
| 1229 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | disable_apic = 1; |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 1231 | clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability); |
| 1232 | return 0; |
| 1233 | } |
| 1234 | early_param("disableapic", setup_disableapic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 1236 | /* same as disableapic, for compatibility */ |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 1237 | static __init int setup_nolapic(char *str) |
| 1238 | { |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 1239 | return setup_disableapic(str); |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 1240 | } |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 1241 | early_param("nolapic", setup_nolapic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | |
Linus Torvalds | 2e7c283 | 2007-03-23 11:32:31 -0700 | [diff] [blame] | 1243 | static int __init parse_lapic_timer_c2_ok(char *arg) |
| 1244 | { |
| 1245 | local_apic_timer_c2_ok = 1; |
| 1246 | return 0; |
| 1247 | } |
| 1248 | early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok); |
| 1249 | |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 1250 | static __init int setup_noapictimer(char *str) |
| 1251 | { |
Andi Kleen | 73dea47 | 2006-02-03 21:50:50 +0100 | [diff] [blame] | 1252 | if (str[0] != ' ' && str[0] != 0) |
OGAWA Hirofumi | 9b41046 | 2006-03-31 02:30:33 -0800 | [diff] [blame] | 1253 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | disable_apic_timer = 1; |
OGAWA Hirofumi | 9b41046 | 2006-03-31 02:30:33 -0800 | [diff] [blame] | 1255 | return 1; |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 1256 | } |
Thomas Gleixner | 9f75e9b | 2007-10-12 23:04:23 +0200 | [diff] [blame] | 1257 | __setup("noapictimer", setup_noapictimer); |
Andi Kleen | 73dea47 | 2006-02-03 21:50:50 +0100 | [diff] [blame] | 1258 | |
Andi Kleen | 0c3749c | 2006-02-03 21:51:41 +0100 | [diff] [blame] | 1259 | static __init int setup_apicpmtimer(char *s) |
| 1260 | { |
| 1261 | apic_calibrate_pmtmr = 1; |
Andi Kleen | 7fd6784 | 2006-02-16 23:42:07 +0100 | [diff] [blame] | 1262 | notsc_setup(NULL); |
Thomas Gleixner | b8ce335 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 1263 | return 0; |
Andi Kleen | 0c3749c | 2006-02-03 21:51:41 +0100 | [diff] [blame] | 1264 | } |
| 1265 | __setup("apicpmtimer", setup_apicpmtimer); |
| 1266 | |