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