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/kernel_stat.h> |
Ingo Molnar | d1de36f | 2009-01-31 01:59:14 +0100 | [diff] [blame^] | 18 | #include <linux/mc146818rtc.h> |
Thomas Gleixner | 70a2002 | 2008-01-30 13:30:18 +0100 | [diff] [blame] | 19 | #include <linux/acpi_pmtmr.h> |
Ingo Molnar | d1de36f | 2009-01-31 01:59:14 +0100 | [diff] [blame^] | 20 | #include <linux/clockchips.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/bootmem.h> |
Frederic Weisbecker | bcbc4f2 | 2008-12-09 23:54:20 +0100 | [diff] [blame] | 23 | #include <linux/ftrace.h> |
Ingo Molnar | d1de36f | 2009-01-31 01:59:14 +0100 | [diff] [blame^] | 24 | #include <linux/ioport.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/sysdev.h> |
| 27 | #include <linux/delay.h> |
Jaswinder Singh Rajput | e423e33 | 2009-01-04 16:16:25 +0530 | [diff] [blame] | 28 | #include <linux/timex.h> |
Ingo Molnar | d1de36f | 2009-01-31 01:59:14 +0100 | [diff] [blame^] | 29 | #include <linux/dmar.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/cpu.h> |
| 32 | #include <linux/dmi.h> |
| 33 | #include <linux/nmi.h> |
| 34 | #include <linux/smp.h> |
| 35 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Yinghai Lu | 773763d | 2008-08-24 02:01:52 -0700 | [diff] [blame] | 37 | #include <asm/arch_hooks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <asm/pgalloc.h> |
Ingo Molnar | d1de36f | 2009-01-31 01:59:14 +0100 | [diff] [blame^] | 39 | #include <asm/genapic.h> |
| 40 | #include <asm/atomic.h> |
| 41 | #include <asm/mpspec.h> |
Yinghai Lu | 773763d | 2008-08-24 02:01:52 -0700 | [diff] [blame] | 42 | #include <asm/i8253.h> |
Ingo Molnar | d1de36f | 2009-01-31 01:59:14 +0100 | [diff] [blame^] | 43 | #include <asm/i8259.h> |
Andi Kleen | 73dea47 | 2006-02-03 21:50:50 +0100 | [diff] [blame] | 44 | #include <asm/proto.h> |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 45 | #include <asm/apic.h> |
Ingo Molnar | d1de36f | 2009-01-31 01:59:14 +0100 | [diff] [blame^] | 46 | #include <asm/desc.h> |
| 47 | #include <asm/hpet.h> |
| 48 | #include <asm/idle.h> |
| 49 | #include <asm/mtrr.h> |
Jaswinder Singh Rajput | 2bc1379 | 2009-01-11 20:34:47 +0530 | [diff] [blame] | 50 | #include <asm/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Cyrill Gorcunov | 80e5609 | 2008-08-24 02:01:42 -0700 | [diff] [blame] | 52 | /* |
| 53 | * Sanity check |
| 54 | */ |
| 55 | #if ((SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F) |
| 56 | # error SPURIOUS_APIC_VECTOR definition error |
| 57 | #endif |
| 58 | |
Brian Gerst | ec70de8 | 2009-01-27 12:56:47 +0900 | [diff] [blame] | 59 | unsigned int num_processors; |
| 60 | unsigned disabled_cpus __cpuinitdata; |
| 61 | /* Processor that is doing the boot up */ |
| 62 | unsigned int boot_cpu_physical_apicid = -1U; |
| 63 | EXPORT_SYMBOL(boot_cpu_physical_apicid); |
| 64 | unsigned int max_physical_apicid; |
| 65 | |
| 66 | /* Bitmask of physically existing CPUs */ |
| 67 | physid_mask_t phys_cpu_present_map; |
| 68 | |
| 69 | /* |
| 70 | * Map cpu index to physical APIC ID |
| 71 | */ |
| 72 | DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID); |
| 73 | DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID); |
| 74 | EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid); |
| 75 | EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid); |
| 76 | |
Yinghai Lu | b3c5117 | 2008-08-24 02:01:46 -0700 | [diff] [blame] | 77 | #ifdef CONFIG_X86_32 |
| 78 | /* |
| 79 | * Knob to control our willingness to enable the local APIC. |
| 80 | * |
| 81 | * +1=force-enable |
| 82 | */ |
| 83 | static int force_enable_local_apic; |
| 84 | /* |
| 85 | * APIC command line parameters |
| 86 | */ |
| 87 | static int __init parse_lapic(char *arg) |
| 88 | { |
| 89 | force_enable_local_apic = 1; |
| 90 | return 0; |
| 91 | } |
| 92 | early_param("lapic", parse_lapic); |
Yinghai Lu | f28c0ae | 2008-08-24 02:01:49 -0700 | [diff] [blame] | 93 | /* Local APIC was disabled by the BIOS and enabled by the kernel */ |
| 94 | static int enabled_via_apicbase; |
| 95 | |
Yinghai Lu | b3c5117 | 2008-08-24 02:01:46 -0700 | [diff] [blame] | 96 | #endif |
| 97 | |
| 98 | #ifdef CONFIG_X86_64 |
Chris Wright | bc1d99c | 2007-10-12 23:04:23 +0200 | [diff] [blame] | 99 | static int apic_calibrate_pmtmr __initdata; |
Yinghai Lu | b3c5117 | 2008-08-24 02:01:46 -0700 | [diff] [blame] | 100 | static __init int setup_apicpmtimer(char *s) |
| 101 | { |
| 102 | apic_calibrate_pmtmr = 1; |
| 103 | notsc_setup(NULL); |
| 104 | return 0; |
| 105 | } |
| 106 | __setup("apicpmtimer", setup_apicpmtimer); |
| 107 | #endif |
| 108 | |
Yinghai Lu | 49899ea | 2008-08-24 02:01:47 -0700 | [diff] [blame] | 109 | #ifdef CONFIG_X86_64 |
| 110 | #define HAVE_X2APIC |
| 111 | #endif |
| 112 | |
| 113 | #ifdef HAVE_X2APIC |
Suresh Siddha | 89027d3 | 2008-07-10 11:16:56 -0700 | [diff] [blame] | 114 | int x2apic; |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 115 | /* x2apic enabled before OS handover */ |
Jaswinder Singh | b6b301a | 2008-12-23 21:52:33 +0530 | [diff] [blame] | 116 | static int x2apic_preenabled; |
| 117 | static int disable_x2apic; |
Yinghai Lu | 49899ea | 2008-08-24 02:01:47 -0700 | [diff] [blame] | 118 | static __init int setup_nox2apic(char *str) |
| 119 | { |
| 120 | disable_x2apic = 1; |
| 121 | setup_clear_cpu_cap(X86_FEATURE_X2APIC); |
| 122 | return 0; |
| 123 | } |
| 124 | early_param("nox2apic", setup_nox2apic); |
| 125 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Yinghai Lu | b3c5117 | 2008-08-24 02:01:46 -0700 | [diff] [blame] | 127 | unsigned long mp_lapic_addr; |
| 128 | int disable_apic; |
| 129 | /* Disable local APIC timer from the kernel commandline or via dmi quirk */ |
| 130 | static int disable_apic_timer __cpuinitdata; |
Hiroshi Shimamoto | e83a5fd | 2008-01-30 13:32:35 +0100 | [diff] [blame] | 131 | /* Local APIC timer works in C2 */ |
Linus Torvalds | 2e7c283 | 2007-03-23 11:32:31 -0700 | [diff] [blame] | 132 | int local_apic_timer_c2_ok; |
| 133 | EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok); |
| 134 | |
Yinghai Lu | efa2559 | 2008-08-19 20:50:36 -0700 | [diff] [blame] | 135 | int first_system_vector = 0xfe; |
| 136 | |
Hiroshi Shimamoto | e83a5fd | 2008-01-30 13:32:35 +0100 | [diff] [blame] | 137 | /* |
| 138 | * Debug level, exported for io_apic.c |
| 139 | */ |
Maciej W. Rozycki | baa1318 | 2008-07-14 18:44:51 +0100 | [diff] [blame] | 140 | unsigned int apic_verbosity; |
Hiroshi Shimamoto | e83a5fd | 2008-01-30 13:32:35 +0100 | [diff] [blame] | 141 | |
Cyrill Gorcunov | 89c38c2 | 2008-08-24 02:01:43 -0700 | [diff] [blame] | 142 | int pic_mode; |
| 143 | |
Alexey Starikovskiy | bab4b27 | 2008-05-19 19:47:03 +0400 | [diff] [blame] | 144 | /* Have we found an MP table */ |
| 145 | int smp_found_config; |
| 146 | |
Aaron Durbin | 3992872 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 147 | static struct resource lapic_resource = { |
| 148 | .name = "Local APIC", |
| 149 | .flags = IORESOURCE_MEM | IORESOURCE_BUSY, |
| 150 | }; |
| 151 | |
Thomas Gleixner | d03030e | 2007-10-12 23:04:06 +0200 | [diff] [blame] | 152 | static unsigned int calibration_result; |
| 153 | |
Thomas Gleixner | ba7eda4 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 154 | static int lapic_next_event(unsigned long delta, |
| 155 | struct clock_event_device *evt); |
| 156 | static void lapic_timer_setup(enum clock_event_mode mode, |
| 157 | struct clock_event_device *evt); |
Mike Travis | 9628937 | 2008-12-31 18:08:46 -0800 | [diff] [blame] | 158 | static void lapic_timer_broadcast(const struct cpumask *mask); |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 159 | static void apic_pm_activate(void); |
Thomas Gleixner | ba7eda4 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 160 | |
Cyrill Gorcunov | 274cfe5 | 2008-08-16 23:21:53 +0400 | [diff] [blame] | 161 | /* |
| 162 | * The local apic timer can be used for any function which is CPU local. |
| 163 | */ |
Thomas Gleixner | ba7eda4 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 164 | static struct clock_event_device lapic_clockevent = { |
| 165 | .name = "lapic", |
| 166 | .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
| 167 | | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY, |
| 168 | .shift = 32, |
| 169 | .set_mode = lapic_timer_setup, |
| 170 | .set_next_event = lapic_next_event, |
| 171 | .broadcast = lapic_timer_broadcast, |
| 172 | .rating = 100, |
| 173 | .irq = -1, |
| 174 | }; |
| 175 | static DEFINE_PER_CPU(struct clock_event_device, lapic_events); |
| 176 | |
Andi Kleen | d343289 | 2008-01-30 13:33:17 +0100 | [diff] [blame] | 177 | static unsigned long apic_phys; |
| 178 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 179 | /* |
| 180 | * Get the LAPIC version |
| 181 | */ |
| 182 | static inline int lapic_get_version(void) |
| 183 | { |
| 184 | return GET_APIC_VERSION(apic_read(APIC_LVR)); |
| 185 | } |
| 186 | |
| 187 | /* |
Cyrill Gorcunov | 9c80386 | 2008-08-16 23:21:54 +0400 | [diff] [blame] | 188 | * Check, if the APIC is integrated or a separate chip |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 189 | */ |
| 190 | static inline int lapic_is_integrated(void) |
| 191 | { |
Cyrill Gorcunov | 9c80386 | 2008-08-16 23:21:54 +0400 | [diff] [blame] | 192 | #ifdef CONFIG_X86_64 |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 193 | return 1; |
Cyrill Gorcunov | 9c80386 | 2008-08-16 23:21:54 +0400 | [diff] [blame] | 194 | #else |
| 195 | return APIC_INTEGRATED(lapic_get_version()); |
| 196 | #endif |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Check, whether this is a modern or a first generation APIC |
| 201 | */ |
| 202 | static int modern_apic(void) |
| 203 | { |
| 204 | /* AMD systems use old APIC versions, so check the CPU */ |
| 205 | if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD && |
| 206 | boot_cpu_data.x86 >= 0xf) |
| 207 | return 1; |
| 208 | return lapic_get_version() >= 0x14; |
| 209 | } |
| 210 | |
Cyrill Gorcunov | 274cfe5 | 2008-08-16 23:21:53 +0400 | [diff] [blame] | 211 | /* |
| 212 | * Paravirt kernels also might be using these below ops. So we still |
| 213 | * use generic apic_read()/apic_write(), which might be pointing to different |
| 214 | * ops in PARAVIRT case. |
| 215 | */ |
Suresh Siddha | 1b374e4 | 2008-07-10 11:16:49 -0700 | [diff] [blame] | 216 | void xapic_wait_icr_idle(void) |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 217 | { |
| 218 | while (apic_read(APIC_ICR) & APIC_ICR_BUSY) |
| 219 | cpu_relax(); |
| 220 | } |
| 221 | |
Suresh Siddha | 1b374e4 | 2008-07-10 11:16:49 -0700 | [diff] [blame] | 222 | u32 safe_xapic_wait_icr_idle(void) |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 223 | { |
| 224 | u32 send_status; |
| 225 | int timeout; |
| 226 | |
| 227 | timeout = 0; |
| 228 | do { |
| 229 | send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY; |
| 230 | if (!send_status) |
| 231 | break; |
| 232 | udelay(100); |
| 233 | } while (timeout++ < 1000); |
| 234 | |
| 235 | return send_status; |
| 236 | } |
| 237 | |
Suresh Siddha | 1b374e4 | 2008-07-10 11:16:49 -0700 | [diff] [blame] | 238 | void xapic_icr_write(u32 low, u32 id) |
| 239 | { |
Cyrill Gorcunov | ed4e5ec | 2008-08-15 13:51:20 +0200 | [diff] [blame] | 240 | apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id)); |
Suresh Siddha | 1b374e4 | 2008-07-10 11:16:49 -0700 | [diff] [blame] | 241 | apic_write(APIC_ICR, low); |
| 242 | } |
| 243 | |
Jaswinder Singh Rajput | ec8c842 | 2008-12-30 22:46:36 +0530 | [diff] [blame] | 244 | static u64 xapic_icr_read(void) |
Suresh Siddha | 1b374e4 | 2008-07-10 11:16:49 -0700 | [diff] [blame] | 245 | { |
| 246 | u32 icr1, icr2; |
| 247 | |
| 248 | icr2 = apic_read(APIC_ICR2); |
| 249 | icr1 = apic_read(APIC_ICR); |
| 250 | |
Cyrill Gorcunov | cf9768d7 | 2008-08-16 23:21:55 +0400 | [diff] [blame] | 251 | return icr1 | ((u64)icr2 << 32); |
Suresh Siddha | 1b374e4 | 2008-07-10 11:16:49 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static struct apic_ops xapic_ops = { |
| 255 | .read = native_apic_mem_read, |
| 256 | .write = native_apic_mem_write, |
Suresh Siddha | 1b374e4 | 2008-07-10 11:16:49 -0700 | [diff] [blame] | 257 | .icr_read = xapic_icr_read, |
| 258 | .icr_write = xapic_icr_write, |
| 259 | .wait_icr_idle = xapic_wait_icr_idle, |
| 260 | .safe_wait_icr_idle = safe_xapic_wait_icr_idle, |
| 261 | }; |
| 262 | |
| 263 | struct apic_ops __read_mostly *apic_ops = &xapic_ops; |
Suresh Siddha | 1b374e4 | 2008-07-10 11:16:49 -0700 | [diff] [blame] | 264 | EXPORT_SYMBOL_GPL(apic_ops); |
| 265 | |
Yinghai Lu | 49899ea | 2008-08-24 02:01:47 -0700 | [diff] [blame] | 266 | #ifdef HAVE_X2APIC |
Suresh Siddha | 13c88fb5 | 2008-07-10 11:16:52 -0700 | [diff] [blame] | 267 | static void x2apic_wait_icr_idle(void) |
| 268 | { |
| 269 | /* no need to wait for icr idle in x2apic */ |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | static u32 safe_x2apic_wait_icr_idle(void) |
| 274 | { |
| 275 | /* no need to wait for icr idle in x2apic */ |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | void x2apic_icr_write(u32 low, u32 id) |
| 280 | { |
| 281 | wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low); |
| 282 | } |
| 283 | |
Jaswinder Singh Rajput | ec8c842 | 2008-12-30 22:46:36 +0530 | [diff] [blame] | 284 | static u64 x2apic_icr_read(void) |
Suresh Siddha | 13c88fb5 | 2008-07-10 11:16:52 -0700 | [diff] [blame] | 285 | { |
| 286 | unsigned long val; |
| 287 | |
| 288 | rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val); |
| 289 | return val; |
| 290 | } |
| 291 | |
| 292 | static struct apic_ops x2apic_ops = { |
| 293 | .read = native_apic_msr_read, |
| 294 | .write = native_apic_msr_write, |
Suresh Siddha | 13c88fb5 | 2008-07-10 11:16:52 -0700 | [diff] [blame] | 295 | .icr_read = x2apic_icr_read, |
| 296 | .icr_write = x2apic_icr_write, |
| 297 | .wait_icr_idle = x2apic_wait_icr_idle, |
| 298 | .safe_wait_icr_idle = safe_x2apic_wait_icr_idle, |
| 299 | }; |
Yinghai Lu | 49899ea | 2008-08-24 02:01:47 -0700 | [diff] [blame] | 300 | #endif |
Suresh Siddha | 13c88fb5 | 2008-07-10 11:16:52 -0700 | [diff] [blame] | 301 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 302 | /** |
| 303 | * enable_NMI_through_LVT0 - enable NMI through local vector table 0 |
| 304 | */ |
Jan Beulich | e942710 | 2008-01-30 13:31:24 +0100 | [diff] [blame] | 305 | void __cpuinit enable_NMI_through_LVT0(void) |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 306 | { |
| 307 | unsigned int v; |
| 308 | |
| 309 | /* unmask and set to NMI */ |
| 310 | v = APIC_DM_NMI; |
Cyrill Gorcunov | d4c63ec | 2008-07-24 13:52:29 +0200 | [diff] [blame] | 311 | |
| 312 | /* Level triggered for 82489DX (32bit mode) */ |
| 313 | if (!lapic_is_integrated()) |
| 314 | v |= APIC_LVT_LEVEL_TRIGGER; |
| 315 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 316 | apic_write(APIC_LVT0, v); |
| 317 | } |
| 318 | |
Cyrill Gorcunov | 7c37e48 | 2008-08-24 02:01:40 -0700 | [diff] [blame] | 319 | #ifdef CONFIG_X86_32 |
| 320 | /** |
| 321 | * get_physical_broadcast - Get number of physical broadcast IDs |
| 322 | */ |
| 323 | int get_physical_broadcast(void) |
| 324 | { |
| 325 | return modern_apic() ? 0xff : 0xf; |
| 326 | } |
| 327 | #endif |
| 328 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 329 | /** |
| 330 | * lapic_get_maxlvt - get the maximum number of local vector table entries |
| 331 | */ |
| 332 | int lapic_get_maxlvt(void) |
| 333 | { |
Cyrill Gorcunov | 36a028d | 2008-07-24 13:52:28 +0200 | [diff] [blame] | 334 | unsigned int v; |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 335 | |
| 336 | v = apic_read(APIC_LVR); |
Cyrill Gorcunov | 36a028d | 2008-07-24 13:52:28 +0200 | [diff] [blame] | 337 | /* |
| 338 | * - we always have APIC integrated on 64bit mode |
| 339 | * - 82489DXs do not report # of LVT entries |
| 340 | */ |
| 341 | return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2; |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* |
Cyrill Gorcunov | 274cfe5 | 2008-08-16 23:21:53 +0400 | [diff] [blame] | 345 | * Local APIC timer |
| 346 | */ |
| 347 | |
Cyrill Gorcunov | c40aaec6 | 2008-08-18 20:45:55 +0400 | [diff] [blame] | 348 | /* Clock divisor */ |
Cyrill Gorcunov | c40aaec6 | 2008-08-18 20:45:55 +0400 | [diff] [blame] | 349 | #define APIC_DIVISOR 16 |
Cyrill Gorcunov | f07f4f9 | 2008-08-15 13:51:21 +0200 | [diff] [blame] | 350 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 351 | /* |
| 352 | * This function sets up the local APIC timer, with a timeout of |
| 353 | * 'clocks' APIC bus clock. During calibration we actually call |
| 354 | * this function twice on the boot CPU, once with a bogus timeout |
| 355 | * value, second time for real. The other (noncalibrating) CPUs |
| 356 | * call this function only once, with the real, calibrated value. |
| 357 | * |
| 358 | * We do reads before writes even if unnecessary, to get around the |
| 359 | * P5 APIC double write bug. |
| 360 | */ |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 361 | static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen) |
| 362 | { |
| 363 | unsigned int lvtt_value, tmp_value; |
| 364 | |
| 365 | lvtt_value = LOCAL_TIMER_VECTOR; |
| 366 | if (!oneshot) |
| 367 | lvtt_value |= APIC_LVT_TIMER_PERIODIC; |
Cyrill Gorcunov | f07f4f9 | 2008-08-15 13:51:21 +0200 | [diff] [blame] | 368 | if (!lapic_is_integrated()) |
| 369 | lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV); |
| 370 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 371 | if (!irqen) |
| 372 | lvtt_value |= APIC_LVT_MASKED; |
| 373 | |
| 374 | apic_write(APIC_LVTT, lvtt_value); |
| 375 | |
| 376 | /* |
| 377 | * Divide PICLK by 16 |
| 378 | */ |
| 379 | tmp_value = apic_read(APIC_TDCR); |
Cyrill Gorcunov | c40aaec6 | 2008-08-18 20:45:55 +0400 | [diff] [blame] | 380 | apic_write(APIC_TDCR, |
| 381 | (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) | |
| 382 | APIC_TDR_DIV_16); |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 383 | |
| 384 | if (!oneshot) |
Cyrill Gorcunov | f07f4f9 | 2008-08-15 13:51:21 +0200 | [diff] [blame] | 385 | apic_write(APIC_TMICT, clocks / APIC_DIVISOR); |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /* |
Robert Richter | 7b83dae | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 389 | * Setup extended LVT, AMD specific (K8, family 10h) |
| 390 | * |
| 391 | * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and |
| 392 | * MCE interrupts are supported. Thus MCE offset must be set to 0. |
Robert Richter | 286f571 | 2008-07-22 21:08:46 +0200 | [diff] [blame] | 393 | * |
| 394 | * If mask=1, the LVT entry does not generate interrupts while mask=0 |
| 395 | * enables the vector. See also the BKDGs. |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 396 | */ |
Robert Richter | 7b83dae | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 397 | |
| 398 | #define APIC_EILVT_LVTOFF_MCE 0 |
| 399 | #define APIC_EILVT_LVTOFF_IBS 1 |
| 400 | |
| 401 | static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask) |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 402 | { |
Robert Richter | 7b83dae | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 403 | unsigned long reg = (lvt_off << 4) + APIC_EILVT0; |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 404 | unsigned int v = (mask << 16) | (msg_type << 8) | vector; |
| 405 | |
| 406 | apic_write(reg, v); |
| 407 | } |
| 408 | |
Robert Richter | 7b83dae | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 409 | u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask) |
| 410 | { |
| 411 | setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask); |
| 412 | return APIC_EILVT_LVTOFF_MCE; |
| 413 | } |
| 414 | |
| 415 | u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask) |
| 416 | { |
| 417 | setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask); |
| 418 | return APIC_EILVT_LVTOFF_IBS; |
| 419 | } |
Robert Richter | 6aa360e | 2008-07-23 15:28:14 +0200 | [diff] [blame] | 420 | EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs); |
Robert Richter | 7b83dae | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 421 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 422 | /* |
| 423 | * Program the next event, relative to now |
| 424 | */ |
Thomas Gleixner | ba7eda4 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 425 | static int lapic_next_event(unsigned long delta, |
| 426 | struct clock_event_device *evt) |
| 427 | { |
| 428 | apic_write(APIC_TMICT, delta); |
| 429 | return 0; |
| 430 | } |
| 431 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 432 | /* |
| 433 | * Setup the lapic timer in periodic or oneshot mode |
| 434 | */ |
Thomas Gleixner | ba7eda4 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 435 | static void lapic_timer_setup(enum clock_event_mode mode, |
| 436 | struct clock_event_device *evt) |
| 437 | { |
| 438 | unsigned long flags; |
| 439 | unsigned int v; |
| 440 | |
| 441 | /* Lapic used as dummy for broadcast ? */ |
| 442 | if (evt->features & CLOCK_EVT_FEAT_DUMMY) |
| 443 | return; |
| 444 | |
| 445 | local_irq_save(flags); |
| 446 | |
| 447 | switch (mode) { |
| 448 | case CLOCK_EVT_MODE_PERIODIC: |
| 449 | case CLOCK_EVT_MODE_ONESHOT: |
| 450 | __setup_APIC_LVTT(calibration_result, |
| 451 | mode != CLOCK_EVT_MODE_PERIODIC, 1); |
| 452 | break; |
| 453 | case CLOCK_EVT_MODE_UNUSED: |
| 454 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 455 | v = apic_read(APIC_LVTT); |
| 456 | v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR); |
| 457 | apic_write(APIC_LVTT, v); |
Thomas Gleixner | a98f8fd | 2008-11-06 01:13:39 +0100 | [diff] [blame] | 458 | apic_write(APIC_TMICT, 0xffffffff); |
Thomas Gleixner | ba7eda4 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 459 | break; |
| 460 | case CLOCK_EVT_MODE_RESUME: |
| 461 | /* Nothing to do here */ |
| 462 | break; |
| 463 | } |
| 464 | |
| 465 | local_irq_restore(flags); |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Local APIC timer broadcast function |
| 470 | */ |
Mike Travis | 9628937 | 2008-12-31 18:08:46 -0800 | [diff] [blame] | 471 | static void lapic_timer_broadcast(const struct cpumask *mask) |
Thomas Gleixner | ba7eda4 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 472 | { |
| 473 | #ifdef CONFIG_SMP |
Ingo Molnar | dac5f41 | 2009-01-28 15:42:24 +0100 | [diff] [blame] | 474 | apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR); |
Thomas Gleixner | ba7eda4 | 2007-10-12 23:04:07 +0200 | [diff] [blame] | 475 | #endif |
| 476 | } |
| 477 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 478 | /* |
| 479 | * Setup the local APIC timer for this CPU. Copy the initilized values |
| 480 | * of the boot CPU and register the clock event in the framework. |
| 481 | */ |
Cyrill Gorcunov | db4b552 | 2008-08-24 02:01:39 -0700 | [diff] [blame] | 482 | static void __cpuinit setup_APIC_timer(void) |
Fernando Luis VazquezCao | 8339e9f | 2007-05-02 19:27:17 +0200 | [diff] [blame] | 483 | { |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 484 | struct clock_event_device *levt = &__get_cpu_var(lapic_events); |
| 485 | |
| 486 | memcpy(levt, &lapic_clockevent, sizeof(*levt)); |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 487 | levt->cpumask = cpumask_of(smp_processor_id()); |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 488 | |
| 489 | clockevents_register_device(levt); |
Fernando Luis VazquezCao | 8339e9f | 2007-05-02 19:27:17 +0200 | [diff] [blame] | 490 | } |
| 491 | |
Yinghai Lu | 2f04fa8 | 2008-08-24 02:01:54 -0700 | [diff] [blame] | 492 | /* |
| 493 | * In this functions we calibrate APIC bus clocks to the external timer. |
| 494 | * |
| 495 | * We want to do the calibration only once since we want to have local timer |
| 496 | * irqs syncron. CPUs connected by the same APIC bus have the very same bus |
| 497 | * frequency. |
| 498 | * |
| 499 | * This was previously done by reading the PIT/HPET and waiting for a wrap |
| 500 | * around to find out, that a tick has elapsed. I have a box, where the PIT |
| 501 | * readout is broken, so it never gets out of the wait loop again. This was |
| 502 | * also reported by others. |
| 503 | * |
| 504 | * Monitoring the jiffies value is inaccurate and the clockevents |
| 505 | * infrastructure allows us to do a simple substitution of the interrupt |
| 506 | * handler. |
| 507 | * |
| 508 | * The calibration routine also uses the pm_timer when possible, as the PIT |
| 509 | * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes |
| 510 | * back to normal later in the boot process). |
| 511 | */ |
| 512 | |
| 513 | #define LAPIC_CAL_LOOPS (HZ/10) |
| 514 | |
| 515 | static __initdata int lapic_cal_loops = -1; |
| 516 | static __initdata long lapic_cal_t1, lapic_cal_t2; |
| 517 | static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2; |
| 518 | static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2; |
| 519 | static __initdata unsigned long lapic_cal_j1, lapic_cal_j2; |
| 520 | |
| 521 | /* |
| 522 | * Temporary interrupt handler. |
| 523 | */ |
| 524 | static void __init lapic_cal_handler(struct clock_event_device *dev) |
| 525 | { |
| 526 | unsigned long long tsc = 0; |
| 527 | long tapic = apic_read(APIC_TMCCT); |
| 528 | unsigned long pm = acpi_pm_read_early(); |
| 529 | |
| 530 | if (cpu_has_tsc) |
| 531 | rdtscll(tsc); |
| 532 | |
| 533 | switch (lapic_cal_loops++) { |
| 534 | case 0: |
| 535 | lapic_cal_t1 = tapic; |
| 536 | lapic_cal_tsc1 = tsc; |
| 537 | lapic_cal_pm1 = pm; |
| 538 | lapic_cal_j1 = jiffies; |
| 539 | break; |
| 540 | |
| 541 | case LAPIC_CAL_LOOPS: |
| 542 | lapic_cal_t2 = tapic; |
| 543 | lapic_cal_tsc2 = tsc; |
| 544 | if (pm < lapic_cal_pm1) |
| 545 | pm += ACPI_PM_OVRRUN; |
| 546 | lapic_cal_pm2 = pm; |
| 547 | lapic_cal_j2 = jiffies; |
| 548 | break; |
| 549 | } |
| 550 | } |
| 551 | |
Cyrill Gorcunov | b189892 | 2008-09-12 23:58:24 +0400 | [diff] [blame] | 552 | static int __init calibrate_by_pmtimer(long deltapm, long *delta) |
| 553 | { |
| 554 | const long pm_100ms = PMTMR_TICKS_PER_SEC / 10; |
| 555 | const long pm_thresh = pm_100ms / 100; |
| 556 | unsigned long mult; |
| 557 | u64 res; |
| 558 | |
| 559 | #ifndef CONFIG_X86_PM_TIMER |
| 560 | return -1; |
| 561 | #endif |
| 562 | |
| 563 | apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm); |
| 564 | |
| 565 | /* Check, if the PM timer is available */ |
| 566 | if (!deltapm) |
| 567 | return -1; |
| 568 | |
| 569 | mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22); |
| 570 | |
| 571 | if (deltapm > (pm_100ms - pm_thresh) && |
| 572 | deltapm < (pm_100ms + pm_thresh)) { |
| 573 | apic_printk(APIC_VERBOSE, "... PM timer result ok\n"); |
| 574 | } else { |
| 575 | res = (((u64)deltapm) * mult) >> 22; |
| 576 | do_div(res, 1000000); |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 577 | pr_warning("APIC calibration not consistent " |
Cyrill Gorcunov | b189892 | 2008-09-12 23:58:24 +0400 | [diff] [blame] | 578 | "with PM Timer: %ldms instead of 100ms\n", |
| 579 | (long)res); |
| 580 | /* Correct the lapic counter value */ |
| 581 | res = (((u64)(*delta)) * pm_100ms); |
| 582 | do_div(res, deltapm); |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 583 | pr_info("APIC delta adjusted to PM-Timer: " |
Cyrill Gorcunov | b189892 | 2008-09-12 23:58:24 +0400 | [diff] [blame] | 584 | "%lu (%ld)\n", (unsigned long)res, *delta); |
| 585 | *delta = (long)res; |
| 586 | } |
| 587 | |
| 588 | return 0; |
| 589 | } |
| 590 | |
Yinghai Lu | 2f04fa8 | 2008-08-24 02:01:54 -0700 | [diff] [blame] | 591 | static int __init calibrate_APIC_clock(void) |
| 592 | { |
| 593 | struct clock_event_device *levt = &__get_cpu_var(lapic_events); |
Yinghai Lu | 2f04fa8 | 2008-08-24 02:01:54 -0700 | [diff] [blame] | 594 | void (*real_handler)(struct clock_event_device *dev); |
| 595 | unsigned long deltaj; |
Cyrill Gorcunov | b189892 | 2008-09-12 23:58:24 +0400 | [diff] [blame] | 596 | long delta; |
Yinghai Lu | 2f04fa8 | 2008-08-24 02:01:54 -0700 | [diff] [blame] | 597 | int pm_referenced = 0; |
| 598 | |
| 599 | local_irq_disable(); |
| 600 | |
| 601 | /* Replace the global interrupt handler */ |
| 602 | real_handler = global_clock_event->event_handler; |
| 603 | global_clock_event->event_handler = lapic_cal_handler; |
| 604 | |
| 605 | /* |
Cyrill Gorcunov | 81608f3 | 2008-10-10 19:00:17 +0400 | [diff] [blame] | 606 | * Setup the APIC counter to maximum. There is no way the lapic |
Yinghai Lu | 2f04fa8 | 2008-08-24 02:01:54 -0700 | [diff] [blame] | 607 | * can underflow in the 100ms detection time frame |
| 608 | */ |
Cyrill Gorcunov | 81608f3 | 2008-10-10 19:00:17 +0400 | [diff] [blame] | 609 | __setup_APIC_LVTT(0xffffffff, 0, 0); |
Yinghai Lu | 2f04fa8 | 2008-08-24 02:01:54 -0700 | [diff] [blame] | 610 | |
| 611 | /* Let the interrupts run */ |
| 612 | local_irq_enable(); |
| 613 | |
| 614 | while (lapic_cal_loops <= LAPIC_CAL_LOOPS) |
| 615 | cpu_relax(); |
| 616 | |
| 617 | local_irq_disable(); |
| 618 | |
| 619 | /* Restore the real event handler */ |
| 620 | global_clock_event->event_handler = real_handler; |
| 621 | |
| 622 | /* Build delta t1-t2 as apic timer counts down */ |
| 623 | delta = lapic_cal_t1 - lapic_cal_t2; |
| 624 | apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta); |
| 625 | |
Cyrill Gorcunov | b189892 | 2008-09-12 23:58:24 +0400 | [diff] [blame] | 626 | /* we trust the PM based calibration if possible */ |
| 627 | pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1, |
| 628 | &delta); |
Yinghai Lu | 2f04fa8 | 2008-08-24 02:01:54 -0700 | [diff] [blame] | 629 | |
| 630 | /* Calculate the scaled math multiplication factor */ |
| 631 | lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS, |
| 632 | lapic_clockevent.shift); |
| 633 | lapic_clockevent.max_delta_ns = |
| 634 | clockevent_delta2ns(0x7FFFFF, &lapic_clockevent); |
| 635 | lapic_clockevent.min_delta_ns = |
| 636 | clockevent_delta2ns(0xF, &lapic_clockevent); |
| 637 | |
| 638 | calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS; |
| 639 | |
| 640 | apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta); |
| 641 | apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult); |
| 642 | apic_printk(APIC_VERBOSE, "..... calibration result: %u\n", |
| 643 | calibration_result); |
| 644 | |
| 645 | if (cpu_has_tsc) { |
| 646 | delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1); |
| 647 | apic_printk(APIC_VERBOSE, "..... CPU clock speed is " |
| 648 | "%ld.%04ld MHz.\n", |
| 649 | (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ), |
| 650 | (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ)); |
| 651 | } |
| 652 | |
| 653 | apic_printk(APIC_VERBOSE, "..... host bus clock speed is " |
| 654 | "%u.%04u MHz.\n", |
| 655 | calibration_result / (1000000 / HZ), |
| 656 | calibration_result % (1000000 / HZ)); |
| 657 | |
| 658 | /* |
| 659 | * Do a sanity check on the APIC calibration result |
| 660 | */ |
| 661 | if (calibration_result < (1000000 / HZ)) { |
| 662 | local_irq_enable(); |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 663 | pr_warning("APIC frequency too slow, disabling apic timer\n"); |
Yinghai Lu | 2f04fa8 | 2008-08-24 02:01:54 -0700 | [diff] [blame] | 664 | return -1; |
| 665 | } |
| 666 | |
| 667 | levt->features &= ~CLOCK_EVT_FEAT_DUMMY; |
| 668 | |
Cyrill Gorcunov | b189892 | 2008-09-12 23:58:24 +0400 | [diff] [blame] | 669 | /* |
| 670 | * PM timer calibration failed or not turned on |
| 671 | * so lets try APIC timer based calibration |
| 672 | */ |
Yinghai Lu | 2f04fa8 | 2008-08-24 02:01:54 -0700 | [diff] [blame] | 673 | if (!pm_referenced) { |
| 674 | apic_printk(APIC_VERBOSE, "... verify APIC timer\n"); |
| 675 | |
| 676 | /* |
| 677 | * Setup the apic timer manually |
| 678 | */ |
| 679 | levt->event_handler = lapic_cal_handler; |
| 680 | lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt); |
| 681 | lapic_cal_loops = -1; |
| 682 | |
| 683 | /* Let the interrupts run */ |
| 684 | local_irq_enable(); |
| 685 | |
| 686 | while (lapic_cal_loops <= LAPIC_CAL_LOOPS) |
| 687 | cpu_relax(); |
| 688 | |
Yinghai Lu | 2f04fa8 | 2008-08-24 02:01:54 -0700 | [diff] [blame] | 689 | /* Stop the lapic timer */ |
| 690 | lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt); |
| 691 | |
Yinghai Lu | 2f04fa8 | 2008-08-24 02:01:54 -0700 | [diff] [blame] | 692 | /* Jiffies delta */ |
| 693 | deltaj = lapic_cal_j2 - lapic_cal_j1; |
| 694 | apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj); |
| 695 | |
| 696 | /* Check, if the jiffies result is consistent */ |
| 697 | if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2) |
| 698 | apic_printk(APIC_VERBOSE, "... jiffies result ok\n"); |
| 699 | else |
| 700 | levt->features |= CLOCK_EVT_FEAT_DUMMY; |
| 701 | } else |
| 702 | local_irq_enable(); |
| 703 | |
| 704 | if (levt->features & CLOCK_EVT_FEAT_DUMMY) { |
Jaswinder Singh Rajput | e423e33 | 2009-01-04 16:16:25 +0530 | [diff] [blame] | 705 | pr_warning("APIC timer disabled due to verification failure\n"); |
Yinghai Lu | 2f04fa8 | 2008-08-24 02:01:54 -0700 | [diff] [blame] | 706 | return -1; |
| 707 | } |
| 708 | |
| 709 | return 0; |
| 710 | } |
| 711 | |
Hiroshi Shimamoto | e83a5fd | 2008-01-30 13:32:35 +0100 | [diff] [blame] | 712 | /* |
| 713 | * Setup the boot APIC |
| 714 | * |
| 715 | * Calibrate and verify the result. |
| 716 | */ |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 717 | void __init setup_boot_APIC_clock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | { |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 719 | /* |
Cyrill Gorcunov | 274cfe5 | 2008-08-16 23:21:53 +0400 | [diff] [blame] | 720 | * The local apic timer can be disabled via the kernel |
| 721 | * commandline or from the CPU detection code. Register the lapic |
| 722 | * timer as a dummy clock event source on SMP systems, so the |
| 723 | * broadcast mechanism is used. On UP systems simply ignore it. |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 724 | */ |
| 725 | if (disable_apic_timer) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 726 | pr_info("Disabling APIC timer\n"); |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 727 | /* No broadcast on UP ! */ |
Thomas Gleixner | 9d09951 | 2008-01-30 13:33:04 +0100 | [diff] [blame] | 728 | if (num_possible_cpus() > 1) { |
| 729 | lapic_clockevent.mult = 1; |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 730 | setup_APIC_timer(); |
Thomas Gleixner | 9d09951 | 2008-01-30 13:33:04 +0100 | [diff] [blame] | 731 | } |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 732 | return; |
| 733 | } |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 734 | |
Cyrill Gorcunov | 274cfe5 | 2008-08-16 23:21:53 +0400 | [diff] [blame] | 735 | apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n" |
| 736 | "calibrating APIC timer ...\n"); |
| 737 | |
Cyrill Gorcunov | 89b3b1f4 | 2008-07-15 21:02:54 +0400 | [diff] [blame] | 738 | if (calibrate_APIC_clock()) { |
Thomas Gleixner | c2b84b3 | 2008-01-30 13:33:04 +0100 | [diff] [blame] | 739 | /* No broadcast on UP ! */ |
| 740 | if (num_possible_cpus() > 1) |
| 741 | setup_APIC_timer(); |
| 742 | return; |
| 743 | } |
| 744 | |
| 745 | /* |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 746 | * If nmi_watchdog is set to IO_APIC, we need the |
| 747 | * PIT/HPET going. Otherwise register lapic as a dummy |
| 748 | * device. |
| 749 | */ |
| 750 | if (nmi_watchdog != NMI_IO_APIC) |
| 751 | lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY; |
| 752 | else |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 753 | pr_warning("APIC timer registered as dummy," |
Cyrill Gorcunov | 116f570 | 2008-06-24 22:52:04 +0200 | [diff] [blame] | 754 | " due to nmi_watchdog=%d!\n", nmi_watchdog); |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 755 | |
Cyrill Gorcunov | 274cfe5 | 2008-08-16 23:21:53 +0400 | [diff] [blame] | 756 | /* Setup the lapic or request the broadcast */ |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 757 | setup_APIC_timer(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 760 | void __cpuinit setup_secondary_APIC_clock(void) |
| 761 | { |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 762 | setup_APIC_timer(); |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | * The guts of the apic timer interrupt |
| 767 | */ |
| 768 | static void local_apic_timer_interrupt(void) |
| 769 | { |
| 770 | int cpu = smp_processor_id(); |
| 771 | struct clock_event_device *evt = &per_cpu(lapic_events, cpu); |
| 772 | |
| 773 | /* |
| 774 | * Normally we should not be here till LAPIC has been initialized but |
| 775 | * in some cases like kdump, its possible that there is a pending LAPIC |
| 776 | * timer interrupt from previous kernel's context and is delivered in |
| 777 | * new kernel the moment interrupts are enabled. |
| 778 | * |
| 779 | * Interrupts are enabled early and LAPIC is setup much later, hence |
| 780 | * its possible that when we get here evt->event_handler is NULL. |
| 781 | * Check for event_handler being NULL and discard the interrupt as |
| 782 | * spurious. |
| 783 | */ |
| 784 | if (!evt->event_handler) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 785 | pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu); |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 786 | /* Switch it off */ |
| 787 | lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt); |
| 788 | return; |
| 789 | } |
| 790 | |
| 791 | /* |
| 792 | * the NMI deadlock-detector uses this. |
| 793 | */ |
Hiroshi Shimamoto | 915b0d0 | 2008-12-08 19:19:26 -0800 | [diff] [blame] | 794 | inc_irq_stat(apic_timer_irqs); |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 795 | |
| 796 | evt->event_handler(evt); |
| 797 | } |
| 798 | |
| 799 | /* |
| 800 | * Local APIC timer interrupt. This is the most natural way for doing |
| 801 | * local interrupts, but local timer interrupts can be emulated by |
| 802 | * broadcast interrupts too. [in case the hw doesn't support APIC timers] |
| 803 | * |
| 804 | * [ if a single-CPU system runs an SMP kernel then we call the local |
| 805 | * interrupt as well. Thus we cannot inline the local irq ... ] |
| 806 | */ |
Frederic Weisbecker | bcbc4f2 | 2008-12-09 23:54:20 +0100 | [diff] [blame] | 807 | void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs) |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 808 | { |
| 809 | struct pt_regs *old_regs = set_irq_regs(regs); |
| 810 | |
| 811 | /* |
| 812 | * NOTE! We'd better ACK the irq immediately, |
| 813 | * because timer handling can be slow. |
| 814 | */ |
| 815 | ack_APIC_irq(); |
| 816 | /* |
| 817 | * update_process_times() expects us to have done irq_enter(). |
| 818 | * Besides, if we don't timer interrupts ignore the global |
| 819 | * interrupt lock, which is the WrongThing (tm) to do. |
| 820 | */ |
| 821 | exit_idle(); |
| 822 | irq_enter(); |
| 823 | local_apic_timer_interrupt(); |
| 824 | irq_exit(); |
Cyrill Gorcunov | 274cfe5 | 2008-08-16 23:21:53 +0400 | [diff] [blame] | 825 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 826 | set_irq_regs(old_regs); |
| 827 | } |
| 828 | |
| 829 | int setup_profiling_timer(unsigned int multiplier) |
| 830 | { |
| 831 | return -EINVAL; |
| 832 | } |
| 833 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 834 | /* |
| 835 | * Local APIC start and shutdown |
| 836 | */ |
| 837 | |
| 838 | /** |
| 839 | * clear_local_APIC - shutdown the local APIC |
| 840 | * |
| 841 | * This is called, when a CPU is disabled and before rebooting, so the state of |
| 842 | * the local APIC has no dangling leftovers. Also used to cleanout any BIOS |
| 843 | * leftovers during boot. |
| 844 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | void clear_local_APIC(void) |
| 846 | { |
Chuck Ebbert | 2584a82 | 2008-05-20 18:18:12 -0400 | [diff] [blame] | 847 | int maxlvt; |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 848 | u32 v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | |
Andi Kleen | d343289 | 2008-01-30 13:33:17 +0100 | [diff] [blame] | 850 | /* APIC hasn't been mapped yet */ |
| 851 | if (!apic_phys) |
| 852 | return; |
| 853 | |
| 854 | maxlvt = lapic_get_maxlvt(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | /* |
Siddha, Suresh B | 704fc59 | 2006-06-26 13:59:53 +0200 | [diff] [blame] | 856 | * Masking an LVT entry can trigger a local APIC error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | * if the vector is zero. Mask LVTERR first to prevent this. |
| 858 | */ |
| 859 | if (maxlvt >= 3) { |
| 860 | v = ERROR_APIC_VECTOR; /* any non-zero vector will do */ |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 861 | apic_write(APIC_LVTERR, v | APIC_LVT_MASKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
| 863 | /* |
| 864 | * Careful: we have to set masks only first to deassert |
| 865 | * any level-triggered sources. |
| 866 | */ |
| 867 | v = apic_read(APIC_LVTT); |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 868 | apic_write(APIC_LVTT, v | APIC_LVT_MASKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | v = apic_read(APIC_LVT0); |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 870 | apic_write(APIC_LVT0, v | APIC_LVT_MASKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | v = apic_read(APIC_LVT1); |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 872 | apic_write(APIC_LVT1, v | APIC_LVT_MASKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | if (maxlvt >= 4) { |
| 874 | v = apic_read(APIC_LVTPC); |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 875 | apic_write(APIC_LVTPC, v | APIC_LVT_MASKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | } |
| 877 | |
Cyrill Gorcunov | 6764014 | 2008-08-16 23:21:50 +0400 | [diff] [blame] | 878 | /* lets not touch this if we didn't frob it */ |
| 879 | #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(X86_MCE_INTEL) |
| 880 | if (maxlvt >= 5) { |
| 881 | v = apic_read(APIC_LVTTHMR); |
| 882 | apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED); |
| 883 | } |
| 884 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | /* |
| 886 | * Clean APIC state for other OSs: |
| 887 | */ |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 888 | apic_write(APIC_LVTT, APIC_LVT_MASKED); |
| 889 | apic_write(APIC_LVT0, APIC_LVT_MASKED); |
| 890 | apic_write(APIC_LVT1, APIC_LVT_MASKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | if (maxlvt >= 3) |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 892 | apic_write(APIC_LVTERR, APIC_LVT_MASKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | if (maxlvt >= 4) |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 894 | apic_write(APIC_LVTPC, APIC_LVT_MASKED); |
Cyrill Gorcunov | 6764014 | 2008-08-16 23:21:50 +0400 | [diff] [blame] | 895 | |
| 896 | /* Integrated APIC (!82489DX) ? */ |
| 897 | if (lapic_is_integrated()) { |
| 898 | if (maxlvt > 3) |
| 899 | /* Clear ESR due to Pentium errata 3AP and 11AP */ |
| 900 | apic_write(APIC_ESR, 0); |
| 901 | apic_read(APIC_ESR); |
| 902 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 905 | /** |
| 906 | * disable_local_APIC - clear and disable the local APIC |
| 907 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | void disable_local_APIC(void) |
| 909 | { |
| 910 | unsigned int value; |
| 911 | |
Jan Beulich | a08c474 | 2009-01-14 12:28:51 +0000 | [diff] [blame] | 912 | /* APIC hasn't been mapped yet */ |
| 913 | if (!apic_phys) |
| 914 | return; |
| 915 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | clear_local_APIC(); |
| 917 | |
| 918 | /* |
| 919 | * Disable APIC (implies clearing of registers |
| 920 | * for 82489DX!). |
| 921 | */ |
| 922 | value = apic_read(APIC_SPIV); |
| 923 | value &= ~APIC_SPIV_APIC_ENABLED; |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 924 | apic_write(APIC_SPIV, value); |
Cyrill Gorcunov | 990b183 | 2008-08-18 20:45:51 +0400 | [diff] [blame] | 925 | |
| 926 | #ifdef CONFIG_X86_32 |
| 927 | /* |
| 928 | * When LAPIC was disabled by the BIOS and enabled by the kernel, |
| 929 | * restore the disabled state. |
| 930 | */ |
| 931 | if (enabled_via_apicbase) { |
| 932 | unsigned int l, h; |
| 933 | |
| 934 | rdmsr(MSR_IA32_APICBASE, l, h); |
| 935 | l &= ~MSR_IA32_APICBASE_ENABLE; |
| 936 | wrmsr(MSR_IA32_APICBASE, l, h); |
| 937 | } |
| 938 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Cyrill Gorcunov | fe4024d | 2008-08-18 20:45:52 +0400 | [diff] [blame] | 941 | /* |
| 942 | * If Linux enabled the LAPIC against the BIOS default disable it down before |
| 943 | * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and |
| 944 | * not power-off. Additionally clear all LVT entries before disable_local_APIC |
| 945 | * for the case where Linux didn't enable the LAPIC. |
| 946 | */ |
Hiroshi Shimamoto | 9b7711f | 2007-10-19 18:21:11 -0700 | [diff] [blame] | 947 | void lapic_shutdown(void) |
| 948 | { |
| 949 | unsigned long flags; |
| 950 | |
| 951 | if (!cpu_has_apic) |
| 952 | return; |
| 953 | |
| 954 | local_irq_save(flags); |
| 955 | |
Cyrill Gorcunov | fe4024d | 2008-08-18 20:45:52 +0400 | [diff] [blame] | 956 | #ifdef CONFIG_X86_32 |
| 957 | if (!enabled_via_apicbase) |
| 958 | clear_local_APIC(); |
| 959 | else |
| 960 | #endif |
| 961 | disable_local_APIC(); |
| 962 | |
Hiroshi Shimamoto | 9b7711f | 2007-10-19 18:21:11 -0700 | [diff] [blame] | 963 | |
| 964 | local_irq_restore(flags); |
| 965 | } |
| 966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | /* |
| 968 | * This is to verify that we're looking at a real local APIC. |
| 969 | * Check these against your board if the CPUs aren't getting |
| 970 | * started for no apparent reason. |
| 971 | */ |
| 972 | int __init verify_local_APIC(void) |
| 973 | { |
| 974 | unsigned int reg0, reg1; |
| 975 | |
| 976 | /* |
| 977 | * The version register is read-only in a real APIC. |
| 978 | */ |
| 979 | reg0 = apic_read(APIC_LVR); |
| 980 | apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0); |
| 981 | apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK); |
| 982 | reg1 = apic_read(APIC_LVR); |
| 983 | apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1); |
| 984 | |
| 985 | /* |
| 986 | * The two version reads above should print the same |
| 987 | * numbers. If the second one is different, then we |
| 988 | * poke at a non-APIC. |
| 989 | */ |
| 990 | if (reg1 != reg0) |
| 991 | return 0; |
| 992 | |
| 993 | /* |
| 994 | * Check if the version looks reasonably. |
| 995 | */ |
| 996 | reg1 = GET_APIC_VERSION(reg0); |
| 997 | if (reg1 == 0x00 || reg1 == 0xff) |
| 998 | return 0; |
Thomas Gleixner | 37e650c | 2008-01-30 13:30:14 +0100 | [diff] [blame] | 999 | reg1 = lapic_get_maxlvt(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | if (reg1 < 0x02 || reg1 == 0xff) |
| 1001 | return 0; |
| 1002 | |
| 1003 | /* |
| 1004 | * The ID register is read/write in a real APIC. |
| 1005 | */ |
Suresh Siddha | 2d7a66d | 2008-07-11 14:24:19 -0700 | [diff] [blame] | 1006 | reg0 = apic_read(APIC_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0); |
Ingo Molnar | 5b81272 | 2009-01-28 14:59:17 +0100 | [diff] [blame] | 1008 | apic_write(APIC_ID, reg0 ^ apic->apic_id_mask); |
Suresh Siddha | 2d7a66d | 2008-07-11 14:24:19 -0700 | [diff] [blame] | 1009 | reg1 = apic_read(APIC_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1); |
| 1011 | apic_write(APIC_ID, reg0); |
Ingo Molnar | 5b81272 | 2009-01-28 14:59:17 +0100 | [diff] [blame] | 1012 | if (reg1 != (reg0 ^ apic->apic_id_mask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | return 0; |
| 1014 | |
| 1015 | /* |
| 1016 | * The next two are just to see if we have sane values. |
| 1017 | * They're only really relevant if we're in Virtual Wire |
| 1018 | * compatibility mode, but most boxes are anymore. |
| 1019 | */ |
| 1020 | reg0 = apic_read(APIC_LVT0); |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1021 | apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | reg1 = apic_read(APIC_LVT1); |
| 1023 | apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1); |
| 1024 | |
| 1025 | return 1; |
| 1026 | } |
| 1027 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1028 | /** |
| 1029 | * sync_Arb_IDs - synchronize APIC bus arbitration IDs |
| 1030 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | void __init sync_Arb_IDs(void) |
| 1032 | { |
Cyrill Gorcunov | 296cb95 | 2008-08-15 13:51:23 +0200 | [diff] [blame] | 1033 | /* |
| 1034 | * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not |
| 1035 | * needed on AMD. |
| 1036 | */ |
| 1037 | if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | return; |
| 1039 | |
| 1040 | /* |
| 1041 | * Wait for idle. |
| 1042 | */ |
| 1043 | apic_wait_icr_idle(); |
| 1044 | |
| 1045 | apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n"); |
Cyrill Gorcunov | 6f6da97 | 2008-08-15 23:05:19 +0400 | [diff] [blame] | 1046 | apic_write(APIC_ICR, APIC_DEST_ALLINC | |
| 1047 | APIC_INT_LEVELTRIG | APIC_DM_INIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | /* |
| 1051 | * An initial setup of the virtual wire mode. |
| 1052 | */ |
| 1053 | void __init init_bsp_APIC(void) |
| 1054 | { |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 1055 | unsigned int value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | |
| 1057 | /* |
| 1058 | * Don't do the setup now if we have a SMP BIOS as the |
| 1059 | * through-I/O-APIC virtual wire mode might be active. |
| 1060 | */ |
| 1061 | if (smp_found_config || !cpu_has_apic) |
| 1062 | return; |
| 1063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | /* |
| 1065 | * Do not trust the local APIC being empty at bootup. |
| 1066 | */ |
| 1067 | clear_local_APIC(); |
| 1068 | |
| 1069 | /* |
| 1070 | * Enable APIC. |
| 1071 | */ |
| 1072 | value = apic_read(APIC_SPIV); |
| 1073 | value &= ~APIC_VECTOR_MASK; |
| 1074 | value |= APIC_SPIV_APIC_ENABLED; |
Cyrill Gorcunov | 638c041 | 2008-08-15 23:05:18 +0400 | [diff] [blame] | 1075 | |
| 1076 | #ifdef CONFIG_X86_32 |
| 1077 | /* This bit is reserved on P4/Xeon and should be cleared */ |
| 1078 | if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && |
| 1079 | (boot_cpu_data.x86 == 15)) |
| 1080 | value &= ~APIC_SPIV_FOCUS_DISABLED; |
| 1081 | else |
| 1082 | #endif |
| 1083 | value |= APIC_SPIV_FOCUS_DISABLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | value |= SPURIOUS_APIC_VECTOR; |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 1085 | apic_write(APIC_SPIV, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
| 1087 | /* |
| 1088 | * Set up the virtual wire mode. |
| 1089 | */ |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 1090 | apic_write(APIC_LVT0, APIC_DM_EXTINT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | value = APIC_DM_NMI; |
Cyrill Gorcunov | 638c041 | 2008-08-15 23:05:18 +0400 | [diff] [blame] | 1092 | if (!lapic_is_integrated()) /* 82489DX */ |
| 1093 | value |= APIC_LVT_LEVEL_TRIGGER; |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 1094 | apic_write(APIC_LVT1, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
Cyrill Gorcunov | c43da2f | 2008-08-18 20:45:54 +0400 | [diff] [blame] | 1097 | static void __cpuinit lapic_setup_esr(void) |
| 1098 | { |
Cyrill Gorcunov | 9df08f1 | 2008-09-14 11:55:37 +0400 | [diff] [blame] | 1099 | unsigned int oldvalue, value, maxlvt; |
Cyrill Gorcunov | c43da2f | 2008-08-18 20:45:54 +0400 | [diff] [blame] | 1100 | |
Cyrill Gorcunov | 9df08f1 | 2008-09-14 11:55:37 +0400 | [diff] [blame] | 1101 | if (!lapic_is_integrated()) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1102 | pr_info("No ESR for 82489DX.\n"); |
Cyrill Gorcunov | 9df08f1 | 2008-09-14 11:55:37 +0400 | [diff] [blame] | 1103 | return; |
Cyrill Gorcunov | c43da2f | 2008-08-18 20:45:54 +0400 | [diff] [blame] | 1104 | } |
Cyrill Gorcunov | 9df08f1 | 2008-09-14 11:55:37 +0400 | [diff] [blame] | 1105 | |
Ingo Molnar | 08125d3 | 2009-01-28 05:08:44 +0100 | [diff] [blame] | 1106 | if (apic->disable_esr) { |
Cyrill Gorcunov | 9df08f1 | 2008-09-14 11:55:37 +0400 | [diff] [blame] | 1107 | /* |
| 1108 | * Something untraceable is creating bad interrupts on |
| 1109 | * secondary quads ... for the moment, just leave the |
| 1110 | * ESR disabled - we can't do anything useful with the |
| 1111 | * errors anyway - mbligh |
| 1112 | */ |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1113 | pr_info("Leaving ESR disabled.\n"); |
Cyrill Gorcunov | 9df08f1 | 2008-09-14 11:55:37 +0400 | [diff] [blame] | 1114 | return; |
| 1115 | } |
| 1116 | |
| 1117 | maxlvt = lapic_get_maxlvt(); |
| 1118 | if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */ |
| 1119 | apic_write(APIC_ESR, 0); |
| 1120 | oldvalue = apic_read(APIC_ESR); |
| 1121 | |
| 1122 | /* enables sending errors */ |
| 1123 | value = ERROR_APIC_VECTOR; |
| 1124 | apic_write(APIC_LVTERR, value); |
| 1125 | |
| 1126 | /* |
| 1127 | * spec says clear errors after enabling vector. |
| 1128 | */ |
| 1129 | if (maxlvt > 3) |
| 1130 | apic_write(APIC_ESR, 0); |
| 1131 | value = apic_read(APIC_ESR); |
| 1132 | if (value != oldvalue) |
| 1133 | apic_printk(APIC_VERBOSE, "ESR value before enabling " |
| 1134 | "vector: 0x%08x after: 0x%08x\n", |
| 1135 | oldvalue, value); |
Cyrill Gorcunov | c43da2f | 2008-08-18 20:45:54 +0400 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1139 | /** |
| 1140 | * setup_local_APIC - setup the local APIC |
| 1141 | */ |
| 1142 | void __cpuinit setup_local_APIC(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | { |
Andi Kleen | 739f33b | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 1144 | unsigned int value; |
Vivek Goyal | da7ed9f | 2006-03-25 16:31:16 +0100 | [diff] [blame] | 1145 | int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | |
Jan Beulich | f118263 | 2009-01-14 12:27:35 +0000 | [diff] [blame] | 1147 | if (disable_apic) { |
Ingo Molnar | 5cdc5e9e | 2009-01-19 20:49:37 +0100 | [diff] [blame] | 1148 | #ifdef CONFIG_X86_IO_APIC |
Jan Beulich | f118263 | 2009-01-14 12:27:35 +0000 | [diff] [blame] | 1149 | disable_ioapic_setup(); |
Ingo Molnar | 5cdc5e9e | 2009-01-19 20:49:37 +0100 | [diff] [blame] | 1150 | #endif |
Jan Beulich | f118263 | 2009-01-14 12:27:35 +0000 | [diff] [blame] | 1151 | return; |
| 1152 | } |
| 1153 | |
Cyrill Gorcunov | 89c38c2 | 2008-08-24 02:01:43 -0700 | [diff] [blame] | 1154 | #ifdef CONFIG_X86_32 |
| 1155 | /* Pound the ESR really hard over the head with a big hammer - mbligh */ |
Ingo Molnar | 08125d3 | 2009-01-28 05:08:44 +0100 | [diff] [blame] | 1156 | if (lapic_is_integrated() && apic->disable_esr) { |
Cyrill Gorcunov | 89c38c2 | 2008-08-24 02:01:43 -0700 | [diff] [blame] | 1157 | apic_write(APIC_ESR, 0); |
| 1158 | apic_write(APIC_ESR, 0); |
| 1159 | apic_write(APIC_ESR, 0); |
| 1160 | apic_write(APIC_ESR, 0); |
| 1161 | } |
| 1162 | #endif |
| 1163 | |
Jack Steiner | ac23d4e | 2008-03-28 14:12:16 -0500 | [diff] [blame] | 1164 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | /* |
| 1167 | * Double-check whether this APIC is really registered. |
| 1168 | * This is meaningless in clustered apic mode, so we skip it. |
| 1169 | */ |
Ingo Molnar | 7ed248d | 2009-01-28 03:43:47 +0100 | [diff] [blame] | 1170 | if (!apic->apic_id_registered()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | BUG(); |
| 1172 | |
| 1173 | /* |
| 1174 | * Intel recommends to set DFR, LDR and TPR before enabling |
| 1175 | * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel |
| 1176 | * document number 292116). So here it goes... |
| 1177 | */ |
Ingo Molnar | a5c4329 | 2009-01-28 06:50:47 +0100 | [diff] [blame] | 1178 | apic->init_apic_ldr(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | |
| 1180 | /* |
| 1181 | * Set Task Priority to 'accept all'. We never change this |
| 1182 | * later on. |
| 1183 | */ |
| 1184 | value = apic_read(APIC_TASKPRI); |
| 1185 | value &= ~APIC_TPRI_MASK; |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 1186 | apic_write(APIC_TASKPRI, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
| 1188 | /* |
Vivek Goyal | da7ed9f | 2006-03-25 16:31:16 +0100 | [diff] [blame] | 1189 | * After a crash, we no longer service the interrupts and a pending |
| 1190 | * interrupt from previous kernel might still have ISR bit set. |
| 1191 | * |
| 1192 | * Most probably by now CPU has serviced that pending interrupt and |
| 1193 | * it might not have done the ack_APIC_irq() because it thought, |
| 1194 | * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it |
| 1195 | * does not clear the ISR bit and cpu thinks it has already serivced |
| 1196 | * the interrupt. Hence a vector might get locked. It was noticed |
| 1197 | * for timer irq (vector 0x31). Issue an extra EOI to clear ISR. |
| 1198 | */ |
| 1199 | for (i = APIC_ISR_NR - 1; i >= 0; i--) { |
| 1200 | value = apic_read(APIC_ISR + i*0x10); |
| 1201 | for (j = 31; j >= 0; j--) { |
| 1202 | if (value & (1<<j)) |
| 1203 | ack_APIC_irq(); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | * Now that we are all set up, enable the APIC |
| 1209 | */ |
| 1210 | value = apic_read(APIC_SPIV); |
| 1211 | value &= ~APIC_VECTOR_MASK; |
| 1212 | /* |
| 1213 | * Enable APIC |
| 1214 | */ |
| 1215 | value |= APIC_SPIV_APIC_ENABLED; |
| 1216 | |
Cyrill Gorcunov | 89c38c2 | 2008-08-24 02:01:43 -0700 | [diff] [blame] | 1217 | #ifdef CONFIG_X86_32 |
| 1218 | /* |
| 1219 | * Some unknown Intel IO/APIC (or APIC) errata is biting us with |
| 1220 | * certain networking cards. If high frequency interrupts are |
| 1221 | * happening on a particular IOAPIC pin, plus the IOAPIC routing |
| 1222 | * entry is masked/unmasked at a high rate as well then sooner or |
| 1223 | * later IOAPIC line gets 'stuck', no more interrupts are received |
| 1224 | * from the device. If focus CPU is disabled then the hang goes |
| 1225 | * away, oh well :-( |
| 1226 | * |
| 1227 | * [ This bug can be reproduced easily with a level-triggered |
| 1228 | * PCI Ne2000 networking cards and PII/PIII processors, dual |
| 1229 | * BX chipset. ] |
| 1230 | */ |
| 1231 | /* |
| 1232 | * Actually disabling the focus CPU check just makes the hang less |
| 1233 | * frequent as it makes the interrupt distributon model be more |
| 1234 | * like LRU than MRU (the short-term load is more even across CPUs). |
| 1235 | * See also the comment in end_level_ioapic_irq(). --macro |
| 1236 | */ |
| 1237 | |
| 1238 | /* |
| 1239 | * - enable focus processor (bit==0) |
| 1240 | * - 64bit mode always use processor focus |
| 1241 | * so no need to set it |
| 1242 | */ |
| 1243 | value &= ~APIC_SPIV_FOCUS_DISABLED; |
| 1244 | #endif |
Andi Kleen | 3f14c74 | 2006-09-26 10:52:29 +0200 | [diff] [blame] | 1245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | /* |
| 1247 | * Set spurious IRQ vector |
| 1248 | */ |
| 1249 | value |= SPURIOUS_APIC_VECTOR; |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 1250 | apic_write(APIC_SPIV, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | |
| 1252 | /* |
| 1253 | * Set up LVT0, LVT1: |
| 1254 | * |
| 1255 | * set up through-local-APIC on the BP's LINT0. This is not |
| 1256 | * strictly necessary in pure symmetric-IO mode, but sometimes |
| 1257 | * we delegate interrupts to the 8259A. |
| 1258 | */ |
| 1259 | /* |
| 1260 | * TODO: set up through-local-APIC from through-I/O-APIC? --macro |
| 1261 | */ |
| 1262 | value = apic_read(APIC_LVT0) & APIC_LVT_MASKED; |
Cyrill Gorcunov | 89c38c2 | 2008-08-24 02:01:43 -0700 | [diff] [blame] | 1263 | if (!smp_processor_id() && (pic_mode || !value)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | value = APIC_DM_EXTINT; |
Chris Wright | bc1d99c | 2007-10-12 23:04:23 +0200 | [diff] [blame] | 1265 | apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", |
Cyrill Gorcunov | 89c38c2 | 2008-08-24 02:01:43 -0700 | [diff] [blame] | 1266 | smp_processor_id()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | } else { |
| 1268 | value = APIC_DM_EXTINT | APIC_LVT_MASKED; |
Chris Wright | bc1d99c | 2007-10-12 23:04:23 +0200 | [diff] [blame] | 1269 | apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", |
Cyrill Gorcunov | 89c38c2 | 2008-08-24 02:01:43 -0700 | [diff] [blame] | 1270 | smp_processor_id()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | } |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 1272 | apic_write(APIC_LVT0, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | |
| 1274 | /* |
| 1275 | * only the BP should see the LINT1 NMI signal, obviously. |
| 1276 | */ |
| 1277 | if (!smp_processor_id()) |
| 1278 | value = APIC_DM_NMI; |
| 1279 | else |
| 1280 | value = APIC_DM_NMI | APIC_LVT_MASKED; |
Cyrill Gorcunov | 89c38c2 | 2008-08-24 02:01:43 -0700 | [diff] [blame] | 1281 | if (!lapic_is_integrated()) /* 82489DX */ |
| 1282 | value |= APIC_LVT_LEVEL_TRIGGER; |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 1283 | apic_write(APIC_LVT1, value); |
Cyrill Gorcunov | 89c38c2 | 2008-08-24 02:01:43 -0700 | [diff] [blame] | 1284 | |
Jack Steiner | ac23d4e | 2008-03-28 14:12:16 -0500 | [diff] [blame] | 1285 | preempt_enable(); |
Andi Kleen | 739f33b | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 1286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | |
Andi Kleen | 739f33b | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 1288 | void __cpuinit end_local_APIC_setup(void) |
| 1289 | { |
| 1290 | lapic_setup_esr(); |
Cyrill Gorcunov | fa6b95f | 2008-08-18 20:45:58 +0400 | [diff] [blame] | 1291 | |
| 1292 | #ifdef CONFIG_X86_32 |
Cyrill Gorcunov | 1b4ee4e | 2008-08-18 23:12:33 +0400 | [diff] [blame] | 1293 | { |
| 1294 | unsigned int value; |
| 1295 | /* Disable the local apic timer */ |
| 1296 | value = apic_read(APIC_LVTT); |
| 1297 | value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR); |
| 1298 | apic_write(APIC_LVTT, value); |
| 1299 | } |
Cyrill Gorcunov | fa6b95f | 2008-08-18 20:45:58 +0400 | [diff] [blame] | 1300 | #endif |
| 1301 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1302 | setup_apic_nmi_watchdog(NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | apic_pm_activate(); |
| 1304 | } |
| 1305 | |
Yinghai Lu | 49899ea | 2008-08-24 02:01:47 -0700 | [diff] [blame] | 1306 | #ifdef HAVE_X2APIC |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1307 | void check_x2apic(void) |
| 1308 | { |
| 1309 | int msr, msr2; |
| 1310 | |
| 1311 | rdmsr(MSR_IA32_APICBASE, msr, msr2); |
| 1312 | |
| 1313 | if (msr & X2APIC_ENABLE) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1314 | pr_info("x2apic enabled by BIOS, switching to x2apic ops\n"); |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1315 | x2apic_preenabled = x2apic = 1; |
| 1316 | apic_ops = &x2apic_ops; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | void enable_x2apic(void) |
| 1321 | { |
| 1322 | int msr, msr2; |
| 1323 | |
| 1324 | rdmsr(MSR_IA32_APICBASE, msr, msr2); |
| 1325 | if (!(msr & X2APIC_ENABLE)) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1326 | pr_info("Enabling x2apic\n"); |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1327 | wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0); |
| 1328 | } |
| 1329 | } |
| 1330 | |
Al Viro | 2236d25 | 2008-11-22 17:37:34 +0000 | [diff] [blame] | 1331 | void __init enable_IR_x2apic(void) |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1332 | { |
| 1333 | #ifdef CONFIG_INTR_REMAP |
| 1334 | int ret; |
| 1335 | unsigned long flags; |
| 1336 | |
| 1337 | if (!cpu_has_x2apic) |
| 1338 | return; |
| 1339 | |
| 1340 | if (!x2apic_preenabled && disable_x2apic) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1341 | pr_info("Skipped enabling x2apic and Interrupt-remapping " |
| 1342 | "because of nox2apic\n"); |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1343 | return; |
| 1344 | } |
| 1345 | |
| 1346 | if (x2apic_preenabled && disable_x2apic) |
| 1347 | panic("Bios already enabled x2apic, can't enforce nox2apic"); |
| 1348 | |
| 1349 | if (!x2apic_preenabled && skip_ioapic_setup) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1350 | pr_info("Skipped enabling x2apic and Interrupt-remapping " |
| 1351 | "because of skipping io-apic setup\n"); |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1352 | return; |
| 1353 | } |
| 1354 | |
| 1355 | ret = dmar_table_init(); |
| 1356 | if (ret) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1357 | pr_info("dmar_table_init() failed with %d:\n", ret); |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1358 | |
| 1359 | if (x2apic_preenabled) |
| 1360 | panic("x2apic enabled by bios. But IR enabling failed"); |
| 1361 | else |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1362 | pr_info("Not enabling x2apic,Intr-remapping\n"); |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1363 | return; |
| 1364 | } |
| 1365 | |
| 1366 | local_irq_save(flags); |
| 1367 | mask_8259A(); |
Cyrill Gorcunov | 5ffa4eb | 2008-09-18 23:37:57 +0400 | [diff] [blame] | 1368 | |
| 1369 | ret = save_mask_IO_APIC_setup(); |
| 1370 | if (ret) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1371 | pr_info("Saving IO-APIC state failed: %d\n", ret); |
Cyrill Gorcunov | 5ffa4eb | 2008-09-18 23:37:57 +0400 | [diff] [blame] | 1372 | goto end; |
| 1373 | } |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1374 | |
| 1375 | ret = enable_intr_remapping(1); |
| 1376 | |
| 1377 | if (ret && x2apic_preenabled) { |
| 1378 | local_irq_restore(flags); |
| 1379 | panic("x2apic enabled by bios. But IR enabling failed"); |
| 1380 | } |
| 1381 | |
| 1382 | if (ret) |
Cyrill Gorcunov | 5ffa4eb | 2008-09-18 23:37:57 +0400 | [diff] [blame] | 1383 | goto end_restore; |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1384 | |
| 1385 | if (!x2apic) { |
| 1386 | x2apic = 1; |
| 1387 | apic_ops = &x2apic_ops; |
| 1388 | enable_x2apic(); |
| 1389 | } |
Cyrill Gorcunov | 5ffa4eb | 2008-09-18 23:37:57 +0400 | [diff] [blame] | 1390 | |
| 1391 | end_restore: |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1392 | if (ret) |
| 1393 | /* |
| 1394 | * IR enabling failed |
| 1395 | */ |
| 1396 | restore_IO_APIC_setup(); |
| 1397 | else |
| 1398 | reinit_intr_remapped_IO_APIC(x2apic_preenabled); |
| 1399 | |
Cyrill Gorcunov | 5ffa4eb | 2008-09-18 23:37:57 +0400 | [diff] [blame] | 1400 | end: |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1401 | unmask_8259A(); |
| 1402 | local_irq_restore(flags); |
| 1403 | |
| 1404 | if (!ret) { |
| 1405 | if (!x2apic_preenabled) |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1406 | pr_info("Enabled x2apic and interrupt-remapping\n"); |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1407 | else |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1408 | pr_info("Enabled Interrupt-remapping\n"); |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1409 | } else |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1410 | pr_err("Failed to enable Interrupt-remapping and x2apic\n"); |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1411 | #else |
| 1412 | if (!cpu_has_x2apic) |
| 1413 | return; |
| 1414 | |
| 1415 | if (x2apic_preenabled) |
| 1416 | panic("x2apic enabled prior OS handover," |
| 1417 | " enable CONFIG_INTR_REMAP"); |
| 1418 | |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1419 | pr_info("Enable CONFIG_INTR_REMAP for enabling intr-remapping " |
| 1420 | " and x2apic\n"); |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1421 | #endif |
| 1422 | |
| 1423 | return; |
| 1424 | } |
Yinghai Lu | 49899ea | 2008-08-24 02:01:47 -0700 | [diff] [blame] | 1425 | #endif /* HAVE_X2APIC */ |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1426 | |
Yinghai Lu | be7a656 | 2008-08-24 02:01:51 -0700 | [diff] [blame] | 1427 | #ifdef CONFIG_X86_64 |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1428 | /* |
| 1429 | * Detect and enable local APICs on non-SMP boards. |
| 1430 | * Original code written by Keir Fraser. |
| 1431 | * On AMD64 we trust the BIOS - if it says no APIC it is likely |
| 1432 | * not correctly set up (usually the APIC timer won't work etc.) |
| 1433 | */ |
| 1434 | static int __init detect_init_APIC(void) |
| 1435 | { |
| 1436 | if (!cpu_has_apic) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1437 | pr_info("No local APIC present\n"); |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1438 | return -1; |
| 1439 | } |
| 1440 | |
| 1441 | mp_lapic_addr = APIC_DEFAULT_PHYS_BASE; |
Glauber de Oliveira Costa | c70dcb7 | 2008-03-19 14:25:58 -0300 | [diff] [blame] | 1442 | boot_cpu_physical_apicid = 0; |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1443 | return 0; |
| 1444 | } |
Yinghai Lu | be7a656 | 2008-08-24 02:01:51 -0700 | [diff] [blame] | 1445 | #else |
| 1446 | /* |
| 1447 | * Detect and initialize APIC |
| 1448 | */ |
| 1449 | static int __init detect_init_APIC(void) |
| 1450 | { |
| 1451 | u32 h, l, features; |
| 1452 | |
| 1453 | /* Disabled by kernel option? */ |
| 1454 | if (disable_apic) |
| 1455 | return -1; |
| 1456 | |
| 1457 | switch (boot_cpu_data.x86_vendor) { |
| 1458 | case X86_VENDOR_AMD: |
| 1459 | if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) || |
| 1460 | (boot_cpu_data.x86 == 15)) |
| 1461 | break; |
| 1462 | goto no_apic; |
| 1463 | case X86_VENDOR_INTEL: |
| 1464 | if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 || |
| 1465 | (boot_cpu_data.x86 == 5 && cpu_has_apic)) |
| 1466 | break; |
| 1467 | goto no_apic; |
| 1468 | default: |
| 1469 | goto no_apic; |
| 1470 | } |
| 1471 | |
| 1472 | if (!cpu_has_apic) { |
| 1473 | /* |
| 1474 | * Over-ride BIOS and try to enable the local APIC only if |
| 1475 | * "lapic" specified. |
| 1476 | */ |
| 1477 | if (!force_enable_local_apic) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1478 | pr_info("Local APIC disabled by BIOS -- " |
| 1479 | "you can enable it with \"lapic\"\n"); |
Yinghai Lu | be7a656 | 2008-08-24 02:01:51 -0700 | [diff] [blame] | 1480 | return -1; |
| 1481 | } |
| 1482 | /* |
| 1483 | * Some BIOSes disable the local APIC in the APIC_BASE |
| 1484 | * MSR. This can only be done in software for Intel P6 or later |
| 1485 | * and AMD K7 (Model > 1) or later. |
| 1486 | */ |
| 1487 | rdmsr(MSR_IA32_APICBASE, l, h); |
| 1488 | if (!(l & MSR_IA32_APICBASE_ENABLE)) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1489 | pr_info("Local APIC disabled by BIOS -- reenabling.\n"); |
Yinghai Lu | be7a656 | 2008-08-24 02:01:51 -0700 | [diff] [blame] | 1490 | l &= ~MSR_IA32_APICBASE_BASE; |
| 1491 | l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE; |
| 1492 | wrmsr(MSR_IA32_APICBASE, l, h); |
| 1493 | enabled_via_apicbase = 1; |
| 1494 | } |
| 1495 | } |
| 1496 | /* |
| 1497 | * The APIC feature bit should now be enabled |
| 1498 | * in `cpuid' |
| 1499 | */ |
| 1500 | features = cpuid_edx(1); |
| 1501 | if (!(features & (1 << X86_FEATURE_APIC))) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1502 | pr_warning("Could not enable APIC!\n"); |
Yinghai Lu | be7a656 | 2008-08-24 02:01:51 -0700 | [diff] [blame] | 1503 | return -1; |
| 1504 | } |
| 1505 | set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC); |
| 1506 | mp_lapic_addr = APIC_DEFAULT_PHYS_BASE; |
| 1507 | |
| 1508 | /* The BIOS may have set up the APIC at some other address */ |
| 1509 | rdmsr(MSR_IA32_APICBASE, l, h); |
| 1510 | if (l & MSR_IA32_APICBASE_ENABLE) |
| 1511 | mp_lapic_addr = l & MSR_IA32_APICBASE_BASE; |
| 1512 | |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1513 | pr_info("Found and enabled local APIC!\n"); |
Yinghai Lu | be7a656 | 2008-08-24 02:01:51 -0700 | [diff] [blame] | 1514 | |
| 1515 | apic_pm_activate(); |
| 1516 | |
| 1517 | return 0; |
| 1518 | |
| 1519 | no_apic: |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1520 | pr_info("No local APIC present or hardware disabled\n"); |
Yinghai Lu | be7a656 | 2008-08-24 02:01:51 -0700 | [diff] [blame] | 1521 | return -1; |
| 1522 | } |
| 1523 | #endif |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1524 | |
Yinghai Lu | f28c0ae | 2008-08-24 02:01:49 -0700 | [diff] [blame] | 1525 | #ifdef CONFIG_X86_64 |
Yinghai Lu | 8643f9d | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 1526 | void __init early_init_lapic_mapping(void) |
| 1527 | { |
Thomas Gleixner | 431ee79 | 2008-05-12 15:43:35 +0200 | [diff] [blame] | 1528 | unsigned long phys_addr; |
Yinghai Lu | 8643f9d | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 1529 | |
| 1530 | /* |
| 1531 | * If no local APIC can be found then go out |
| 1532 | * : it means there is no mpatable and MADT |
| 1533 | */ |
| 1534 | if (!smp_found_config) |
| 1535 | return; |
| 1536 | |
Thomas Gleixner | 431ee79 | 2008-05-12 15:43:35 +0200 | [diff] [blame] | 1537 | phys_addr = mp_lapic_addr; |
Yinghai Lu | 8643f9d | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 1538 | |
Thomas Gleixner | 431ee79 | 2008-05-12 15:43:35 +0200 | [diff] [blame] | 1539 | set_fixmap_nocache(FIX_APIC_BASE, phys_addr); |
Yinghai Lu | 8643f9d | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 1540 | apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n", |
Thomas Gleixner | 431ee79 | 2008-05-12 15:43:35 +0200 | [diff] [blame] | 1541 | APIC_BASE, phys_addr); |
Yinghai Lu | 8643f9d | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 1542 | |
| 1543 | /* |
| 1544 | * Fetch the APIC ID of the BSP in case we have a |
| 1545 | * default configuration (or the MP table is broken). |
| 1546 | */ |
Yinghai Lu | 4c9961d | 2008-07-11 18:44:16 -0700 | [diff] [blame] | 1547 | boot_cpu_physical_apicid = read_apic_id(); |
Yinghai Lu | 8643f9d | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 1548 | } |
Yinghai Lu | f28c0ae | 2008-08-24 02:01:49 -0700 | [diff] [blame] | 1549 | #endif |
Yinghai Lu | 8643f9d | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 1550 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1551 | /** |
| 1552 | * init_apic_mappings - initialize APIC mappings |
| 1553 | */ |
| 1554 | void __init init_apic_mappings(void) |
| 1555 | { |
Yinghai Lu | 49899ea | 2008-08-24 02:01:47 -0700 | [diff] [blame] | 1556 | #ifdef HAVE_X2APIC |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1557 | if (x2apic) { |
Yinghai Lu | 4c9961d | 2008-07-11 18:44:16 -0700 | [diff] [blame] | 1558 | boot_cpu_physical_apicid = read_apic_id(); |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1559 | return; |
| 1560 | } |
Yinghai Lu | 49899ea | 2008-08-24 02:01:47 -0700 | [diff] [blame] | 1561 | #endif |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1562 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1563 | /* |
| 1564 | * If no local APIC can be found then set up a fake all |
| 1565 | * zeroes page to simulate the local APIC and another |
| 1566 | * one for the IO-APIC. |
| 1567 | */ |
| 1568 | if (!smp_found_config && detect_init_APIC()) { |
| 1569 | apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE); |
| 1570 | apic_phys = __pa(apic_phys); |
| 1571 | } else |
| 1572 | apic_phys = mp_lapic_addr; |
| 1573 | |
| 1574 | set_fixmap_nocache(FIX_APIC_BASE, apic_phys); |
Yinghai Lu | 79c0969 | 2008-09-07 17:58:57 -0700 | [diff] [blame] | 1575 | apic_printk(APIC_VERBOSE, "mapped APIC to %08lx (%08lx)\n", |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1576 | APIC_BASE, apic_phys); |
| 1577 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1578 | /* |
| 1579 | * Fetch the APIC ID of the BSP in case we have a |
| 1580 | * default configuration (or the MP table is broken). |
| 1581 | */ |
Yinghai Lu | f28c0ae | 2008-08-24 02:01:49 -0700 | [diff] [blame] | 1582 | if (boot_cpu_physical_apicid == -1U) |
| 1583 | boot_cpu_physical_apicid = read_apic_id(); |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | /* |
| 1587 | * This initializes the IO-APIC and APIC hardware if this is |
| 1588 | * a UP kernel. |
| 1589 | */ |
Cyrill Gorcunov | 1b313f4 | 2008-08-18 20:45:57 +0400 | [diff] [blame] | 1590 | int apic_version[MAX_APICS]; |
| 1591 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1592 | int __init APIC_init_uniprocessor(void) |
| 1593 | { |
| 1594 | if (disable_apic) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1595 | pr_info("Apic disabled\n"); |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1596 | return -1; |
| 1597 | } |
Jan Beulich | f118263 | 2009-01-14 12:27:35 +0000 | [diff] [blame] | 1598 | #ifdef CONFIG_X86_64 |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1599 | if (!cpu_has_apic) { |
| 1600 | disable_apic = 1; |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1601 | pr_info("Apic disabled by BIOS\n"); |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1602 | return -1; |
| 1603 | } |
Yinghai Lu | fa2bd35 | 2008-08-24 02:01:50 -0700 | [diff] [blame] | 1604 | #else |
| 1605 | if (!smp_found_config && !cpu_has_apic) |
| 1606 | return -1; |
| 1607 | |
| 1608 | /* |
| 1609 | * Complain if the BIOS pretends there is one. |
| 1610 | */ |
| 1611 | if (!cpu_has_apic && |
| 1612 | APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1613 | pr_err("BIOS bug, local APIC 0x%x not detected!...\n", |
| 1614 | boot_cpu_physical_apicid); |
Yinghai Lu | fa2bd35 | 2008-08-24 02:01:50 -0700 | [diff] [blame] | 1615 | clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC); |
| 1616 | return -1; |
| 1617 | } |
| 1618 | #endif |
| 1619 | |
Yinghai Lu | 49899ea | 2008-08-24 02:01:47 -0700 | [diff] [blame] | 1620 | #ifdef HAVE_X2APIC |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1621 | enable_IR_x2apic(); |
Yinghai Lu | 49899ea | 2008-08-24 02:01:47 -0700 | [diff] [blame] | 1622 | #endif |
Yinghai Lu | fa2bd35 | 2008-08-24 02:01:50 -0700 | [diff] [blame] | 1623 | #ifdef CONFIG_X86_64 |
Ingo Molnar | 72ce016 | 2009-01-28 06:50:47 +0100 | [diff] [blame] | 1624 | default_setup_apic_routing(); |
Yinghai Lu | fa2bd35 | 2008-08-24 02:01:50 -0700 | [diff] [blame] | 1625 | #endif |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 1626 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1627 | verify_local_APIC(); |
Glauber Costa | b584176 | 2008-05-28 13:38:28 -0300 | [diff] [blame] | 1628 | connect_bsp_APIC(); |
| 1629 | |
Yinghai Lu | fa2bd35 | 2008-08-24 02:01:50 -0700 | [diff] [blame] | 1630 | #ifdef CONFIG_X86_64 |
Glauber de Oliveira Costa | c70dcb7 | 2008-03-19 14:25:58 -0300 | [diff] [blame] | 1631 | apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid)); |
Yinghai Lu | fa2bd35 | 2008-08-24 02:01:50 -0700 | [diff] [blame] | 1632 | #else |
| 1633 | /* |
| 1634 | * Hack: In case of kdump, after a crash, kernel might be booting |
| 1635 | * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid |
| 1636 | * might be zero if read from MP tables. Get it from LAPIC. |
| 1637 | */ |
| 1638 | # ifdef CONFIG_CRASH_DUMP |
| 1639 | boot_cpu_physical_apicid = read_apic_id(); |
| 1640 | # endif |
| 1641 | #endif |
| 1642 | physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map); |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1643 | setup_local_APIC(); |
| 1644 | |
Yinghai Lu | fa2bd35 | 2008-08-24 02:01:50 -0700 | [diff] [blame] | 1645 | #ifdef CONFIG_X86_64 |
Andi Kleen | 739f33b | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 1646 | /* |
| 1647 | * Now enable IO-APICs, actually call clear_IO_APIC |
| 1648 | * We need clear_IO_APIC before enabling vector on BP |
| 1649 | */ |
| 1650 | if (!skip_ioapic_setup && nr_ioapics) |
| 1651 | enable_IO_APIC(); |
Yinghai Lu | fa2bd35 | 2008-08-24 02:01:50 -0700 | [diff] [blame] | 1652 | #endif |
Andi Kleen | 739f33b | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 1653 | |
Yinghai Lu | fa2bd35 | 2008-08-24 02:01:50 -0700 | [diff] [blame] | 1654 | #ifdef CONFIG_X86_IO_APIC |
Maciej W. Rozycki | acae7d9 | 2008-06-06 03:27:49 +0100 | [diff] [blame] | 1655 | if (!smp_found_config || skip_ioapic_setup || !nr_ioapics) |
Yinghai Lu | fa2bd35 | 2008-08-24 02:01:50 -0700 | [diff] [blame] | 1656 | #endif |
Maciej W. Rozycki | acae7d9 | 2008-06-06 03:27:49 +0100 | [diff] [blame] | 1657 | localise_nmi_watchdog(); |
Andi Kleen | 739f33b | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 1658 | end_local_APIC_setup(); |
| 1659 | |
Yinghai Lu | fa2bd35 | 2008-08-24 02:01:50 -0700 | [diff] [blame] | 1660 | #ifdef CONFIG_X86_IO_APIC |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1661 | if (smp_found_config && !skip_ioapic_setup && nr_ioapics) |
| 1662 | setup_IO_APIC(); |
Yinghai Lu | fa2bd35 | 2008-08-24 02:01:50 -0700 | [diff] [blame] | 1663 | # ifdef CONFIG_X86_64 |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1664 | else |
| 1665 | nr_ioapics = 0; |
Yinghai Lu | fa2bd35 | 2008-08-24 02:01:50 -0700 | [diff] [blame] | 1666 | # endif |
| 1667 | #endif |
| 1668 | |
| 1669 | #ifdef CONFIG_X86_64 |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1670 | setup_boot_APIC_clock(); |
| 1671 | check_nmi_watchdog(); |
Yinghai Lu | fa2bd35 | 2008-08-24 02:01:50 -0700 | [diff] [blame] | 1672 | #else |
| 1673 | setup_boot_clock(); |
| 1674 | #endif |
| 1675 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1676 | return 0; |
| 1677 | } |
| 1678 | |
| 1679 | /* |
| 1680 | * Local APIC interrupts |
| 1681 | */ |
| 1682 | |
| 1683 | /* |
| 1684 | * This interrupt should _never_ happen with our APIC/SMP architecture |
| 1685 | */ |
Yinghai Lu | dc1528d | 2008-08-24 02:01:53 -0700 | [diff] [blame] | 1686 | void smp_spurious_interrupt(struct pt_regs *regs) |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1687 | { |
Yinghai Lu | dc1528d | 2008-08-24 02:01:53 -0700 | [diff] [blame] | 1688 | u32 v; |
| 1689 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1690 | exit_idle(); |
| 1691 | irq_enter(); |
| 1692 | /* |
| 1693 | * Check if this really is a spurious interrupt and ACK it |
| 1694 | * if it is a vectored one. Just in case... |
| 1695 | * Spurious interrupts should not be ACKed. |
| 1696 | */ |
| 1697 | v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1)); |
| 1698 | if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f))) |
| 1699 | ack_APIC_irq(); |
| 1700 | |
Hiroshi Shimamoto | 915b0d0 | 2008-12-08 19:19:26 -0800 | [diff] [blame] | 1701 | inc_irq_stat(irq_spurious_count); |
| 1702 | |
Yinghai Lu | dc1528d | 2008-08-24 02:01:53 -0700 | [diff] [blame] | 1703 | /* see sw-dev-man vol 3, chapter 7.4.13.5 */ |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1704 | pr_info("spurious APIC interrupt on CPU#%d, " |
| 1705 | "should never happen.\n", smp_processor_id()); |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1706 | irq_exit(); |
| 1707 | } |
| 1708 | |
| 1709 | /* |
| 1710 | * This interrupt should never happen with our APIC/SMP architecture |
| 1711 | */ |
Yinghai Lu | dc1528d | 2008-08-24 02:01:53 -0700 | [diff] [blame] | 1712 | void smp_error_interrupt(struct pt_regs *regs) |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1713 | { |
Yinghai Lu | dc1528d | 2008-08-24 02:01:53 -0700 | [diff] [blame] | 1714 | u32 v, v1; |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1715 | |
| 1716 | exit_idle(); |
| 1717 | irq_enter(); |
| 1718 | /* First tickle the hardware, only then report what went on. -- REW */ |
| 1719 | v = apic_read(APIC_ESR); |
| 1720 | apic_write(APIC_ESR, 0); |
| 1721 | v1 = apic_read(APIC_ESR); |
| 1722 | ack_APIC_irq(); |
| 1723 | atomic_inc(&irq_err_count); |
| 1724 | |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1725 | /* |
| 1726 | * Here is what the APIC error bits mean: |
| 1727 | * 0: Send CS error |
| 1728 | * 1: Receive CS error |
| 1729 | * 2: Send accept error |
| 1730 | * 3: Receive accept error |
| 1731 | * 4: Reserved |
| 1732 | * 5: Send illegal vector |
| 1733 | * 6: Received illegal vector |
| 1734 | * 7: Illegal register address |
| 1735 | */ |
| 1736 | pr_debug("APIC error on CPU%d: %02x(%02x)\n", |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1737 | smp_processor_id(), v , v1); |
| 1738 | irq_exit(); |
| 1739 | } |
| 1740 | |
Glauber Costa | b584176 | 2008-05-28 13:38:28 -0300 | [diff] [blame] | 1741 | /** |
Cyrill Gorcunov | 36c9d67 | 2008-08-18 20:45:53 +0400 | [diff] [blame] | 1742 | * connect_bsp_APIC - attach the APIC to the interrupt system |
| 1743 | */ |
Glauber Costa | b584176 | 2008-05-28 13:38:28 -0300 | [diff] [blame] | 1744 | void __init connect_bsp_APIC(void) |
| 1745 | { |
Cyrill Gorcunov | 36c9d67 | 2008-08-18 20:45:53 +0400 | [diff] [blame] | 1746 | #ifdef CONFIG_X86_32 |
| 1747 | if (pic_mode) { |
| 1748 | /* |
| 1749 | * Do not trust the local APIC being empty at bootup. |
| 1750 | */ |
| 1751 | clear_local_APIC(); |
| 1752 | /* |
| 1753 | * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's |
| 1754 | * local APIC to INT and NMI lines. |
| 1755 | */ |
| 1756 | apic_printk(APIC_VERBOSE, "leaving PIC mode, " |
| 1757 | "enabling APIC mode.\n"); |
| 1758 | outb(0x70, 0x22); |
| 1759 | outb(0x01, 0x23); |
| 1760 | } |
| 1761 | #endif |
Ingo Molnar | 4904033 | 2009-01-28 12:43:18 +0100 | [diff] [blame] | 1762 | if (apic->enable_apic_mode) |
| 1763 | apic->enable_apic_mode(); |
Glauber Costa | b584176 | 2008-05-28 13:38:28 -0300 | [diff] [blame] | 1764 | } |
| 1765 | |
Cyrill Gorcunov | 274cfe5 | 2008-08-16 23:21:53 +0400 | [diff] [blame] | 1766 | /** |
| 1767 | * disconnect_bsp_APIC - detach the APIC from the interrupt system |
| 1768 | * @virt_wire_setup: indicates, whether virtual wire mode is selected |
| 1769 | * |
| 1770 | * Virtual wire mode is necessary to deliver legacy interrupts even when the |
| 1771 | * APIC is disabled. |
| 1772 | */ |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1773 | void disconnect_bsp_APIC(int virt_wire_setup) |
| 1774 | { |
Cyrill Gorcunov | 1b4ee4e | 2008-08-18 23:12:33 +0400 | [diff] [blame] | 1775 | unsigned int value; |
| 1776 | |
Cyrill Gorcunov | c177b0b | 2008-08-18 20:45:56 +0400 | [diff] [blame] | 1777 | #ifdef CONFIG_X86_32 |
| 1778 | if (pic_mode) { |
| 1779 | /* |
| 1780 | * Put the board back into PIC mode (has an effect only on |
| 1781 | * certain older boards). Note that APIC interrupts, including |
| 1782 | * IPIs, won't work beyond this point! The only exception are |
| 1783 | * INIT IPIs. |
| 1784 | */ |
| 1785 | apic_printk(APIC_VERBOSE, "disabling APIC mode, " |
| 1786 | "entering PIC mode.\n"); |
| 1787 | outb(0x70, 0x22); |
| 1788 | outb(0x00, 0x23); |
| 1789 | return; |
| 1790 | } |
| 1791 | #endif |
| 1792 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1793 | /* Go back to Virtual Wire compatibility mode */ |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1794 | |
| 1795 | /* For the spurious interrupt use vector F, and enable it */ |
| 1796 | value = apic_read(APIC_SPIV); |
| 1797 | value &= ~APIC_VECTOR_MASK; |
| 1798 | value |= APIC_SPIV_APIC_ENABLED; |
| 1799 | value |= 0xf; |
| 1800 | apic_write(APIC_SPIV, value); |
| 1801 | |
| 1802 | if (!virt_wire_setup) { |
| 1803 | /* |
| 1804 | * For LVT0 make it edge triggered, active high, |
| 1805 | * external and enabled |
| 1806 | */ |
| 1807 | value = apic_read(APIC_LVT0); |
| 1808 | value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING | |
| 1809 | APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR | |
| 1810 | APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED); |
| 1811 | value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING; |
| 1812 | value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT); |
| 1813 | apic_write(APIC_LVT0, value); |
| 1814 | } else { |
| 1815 | /* Disable LVT0 */ |
| 1816 | apic_write(APIC_LVT0, APIC_LVT_MASKED); |
| 1817 | } |
| 1818 | |
Cyrill Gorcunov | c177b0b | 2008-08-18 20:45:56 +0400 | [diff] [blame] | 1819 | /* |
| 1820 | * For LVT1 make it edge triggered, active high, |
| 1821 | * nmi and enabled |
| 1822 | */ |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1823 | value = apic_read(APIC_LVT1); |
| 1824 | value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING | |
| 1825 | APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR | |
| 1826 | APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED); |
| 1827 | value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING; |
| 1828 | value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI); |
| 1829 | apic_write(APIC_LVT1, value); |
| 1830 | } |
| 1831 | |
Alexey Starikovskiy | be8a568 | 2008-03-27 23:56:19 +0300 | [diff] [blame] | 1832 | void __cpuinit generic_processor_info(int apicid, int version) |
| 1833 | { |
| 1834 | int cpu; |
Alexey Starikovskiy | be8a568 | 2008-03-27 23:56:19 +0300 | [diff] [blame] | 1835 | |
Cyrill Gorcunov | 1b313f4 | 2008-08-18 20:45:57 +0400 | [diff] [blame] | 1836 | /* |
| 1837 | * Validate version |
| 1838 | */ |
| 1839 | if (version == 0x0) { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 1840 | pr_warning("BIOS bug, APIC version is 0 for CPU#%d! " |
Mike Travis | 3b11ce7 | 2008-12-17 15:21:39 -0800 | [diff] [blame] | 1841 | "fixing up to 0x10. (tell your hw vendor)\n", |
| 1842 | version); |
Cyrill Gorcunov | 1b313f4 | 2008-08-18 20:45:57 +0400 | [diff] [blame] | 1843 | version = 0x10; |
| 1844 | } |
| 1845 | apic_version[apicid] = version; |
| 1846 | |
Mike Travis | 3b11ce7 | 2008-12-17 15:21:39 -0800 | [diff] [blame] | 1847 | if (num_processors >= nr_cpu_ids) { |
| 1848 | int max = nr_cpu_ids; |
| 1849 | int thiscpu = max + disabled_cpus; |
| 1850 | |
| 1851 | pr_warning( |
| 1852 | "ACPI: NR_CPUS/possible_cpus limit of %i reached." |
| 1853 | " Processor %d/0x%x ignored.\n", max, thiscpu, apicid); |
| 1854 | |
| 1855 | disabled_cpus++; |
Alexey Starikovskiy | be8a568 | 2008-03-27 23:56:19 +0300 | [diff] [blame] | 1856 | return; |
| 1857 | } |
| 1858 | |
| 1859 | num_processors++; |
Mike Travis | 3b11ce7 | 2008-12-17 15:21:39 -0800 | [diff] [blame] | 1860 | cpu = cpumask_next_zero(-1, cpu_present_mask); |
Alexey Starikovskiy | be8a568 | 2008-03-27 23:56:19 +0300 | [diff] [blame] | 1861 | |
Mike Travis | cef30b3 | 2009-01-16 15:58:13 -0800 | [diff] [blame] | 1862 | if (version != apic_version[boot_cpu_physical_apicid]) |
| 1863 | WARN_ONCE(1, |
| 1864 | "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n", |
| 1865 | apic_version[boot_cpu_physical_apicid], cpu, version); |
| 1866 | |
Alexey Starikovskiy | be8a568 | 2008-03-27 23:56:19 +0300 | [diff] [blame] | 1867 | physid_set(apicid, phys_cpu_present_map); |
| 1868 | if (apicid == boot_cpu_physical_apicid) { |
| 1869 | /* |
| 1870 | * x86_bios_cpu_apicid is required to have processors listed |
| 1871 | * in same order as logical cpu numbers. Hence the first |
| 1872 | * entry is BSP, and so on. |
| 1873 | */ |
| 1874 | cpu = 0; |
| 1875 | } |
Yinghai Lu | e0da336 | 2008-06-08 18:29:22 -0700 | [diff] [blame] | 1876 | if (apicid > max_physical_apicid) |
| 1877 | max_physical_apicid = apicid; |
| 1878 | |
Cyrill Gorcunov | 1b313f4 | 2008-08-18 20:45:57 +0400 | [diff] [blame] | 1879 | #ifdef CONFIG_X86_32 |
| 1880 | /* |
| 1881 | * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y |
| 1882 | * but we need to work other dependencies like SMP_SUSPEND etc |
| 1883 | * before this can be done without some confusion. |
| 1884 | * if (CPU_HOTPLUG_ENABLED || num_processors > 8) |
| 1885 | * - Ashok Raj <ashok.raj@intel.com> |
| 1886 | */ |
| 1887 | if (max_physical_apicid >= 8) { |
| 1888 | switch (boot_cpu_data.x86_vendor) { |
| 1889 | case X86_VENDOR_INTEL: |
| 1890 | if (!APIC_XAPIC(version)) { |
| 1891 | def_to_bigsmp = 0; |
| 1892 | break; |
| 1893 | } |
| 1894 | /* If P4 and above fall through */ |
| 1895 | case X86_VENDOR_AMD: |
| 1896 | def_to_bigsmp = 1; |
| 1897 | } |
| 1898 | } |
| 1899 | #endif |
| 1900 | |
Ingo Molnar | 3e5095d | 2009-01-27 17:07:08 +0100 | [diff] [blame] | 1901 | #if defined(CONFIG_SMP) || defined(CONFIG_X86_64) |
Tejun Heo | f10fcd47 | 2009-01-13 20:41:34 +0900 | [diff] [blame] | 1902 | early_per_cpu(x86_cpu_to_apicid, cpu) = apicid; |
| 1903 | early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid; |
Cyrill Gorcunov | 1b313f4 | 2008-08-18 20:45:57 +0400 | [diff] [blame] | 1904 | #endif |
Alexey Starikovskiy | be8a568 | 2008-03-27 23:56:19 +0300 | [diff] [blame] | 1905 | |
Mike Travis | 1de88cd | 2008-12-16 17:34:02 -0800 | [diff] [blame] | 1906 | set_cpu_possible(cpu, true); |
| 1907 | set_cpu_present(cpu, true); |
Alexey Starikovskiy | be8a568 | 2008-03-27 23:56:19 +0300 | [diff] [blame] | 1908 | } |
| 1909 | |
Suresh Siddha | 0c81c74 | 2008-07-10 11:16:48 -0700 | [diff] [blame] | 1910 | int hard_smp_processor_id(void) |
| 1911 | { |
| 1912 | return read_apic_id(); |
| 1913 | } |
Ingo Molnar | 1dcdd3d | 2009-01-28 17:55:37 +0100 | [diff] [blame] | 1914 | |
| 1915 | void default_init_apic_ldr(void) |
| 1916 | { |
| 1917 | unsigned long val; |
| 1918 | |
| 1919 | apic_write(APIC_DFR, APIC_DFR_VALUE); |
| 1920 | val = apic_read(APIC_LDR) & ~APIC_LDR_MASK; |
| 1921 | val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id()); |
| 1922 | apic_write(APIC_LDR, val); |
| 1923 | } |
| 1924 | |
| 1925 | #ifdef CONFIG_X86_32 |
| 1926 | int default_apicid_to_node(int logical_apicid) |
| 1927 | { |
| 1928 | #ifdef CONFIG_SMP |
| 1929 | return apicid_2_node[hard_smp_processor_id()]; |
| 1930 | #else |
| 1931 | return 0; |
| 1932 | #endif |
| 1933 | } |
Yinghai Lu | 3491998 | 2008-08-24 02:01:48 -0700 | [diff] [blame] | 1934 | #endif |
Suresh Siddha | 0c81c74 | 2008-07-10 11:16:48 -0700 | [diff] [blame] | 1935 | |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 1936 | /* |
| 1937 | * Power management |
| 1938 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | #ifdef CONFIG_PM |
| 1940 | |
| 1941 | static struct { |
Cyrill Gorcunov | 274cfe5 | 2008-08-16 23:21:53 +0400 | [diff] [blame] | 1942 | /* |
| 1943 | * 'active' is true if the local APIC was enabled by us and |
| 1944 | * not the BIOS; this signifies that we are also responsible |
| 1945 | * for disabling it before entering apm/acpi suspend |
| 1946 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | int active; |
| 1948 | /* r/w apic fields */ |
| 1949 | unsigned int apic_id; |
| 1950 | unsigned int apic_taskpri; |
| 1951 | unsigned int apic_ldr; |
| 1952 | unsigned int apic_dfr; |
| 1953 | unsigned int apic_spiv; |
| 1954 | unsigned int apic_lvtt; |
| 1955 | unsigned int apic_lvtpc; |
| 1956 | unsigned int apic_lvt0; |
| 1957 | unsigned int apic_lvt1; |
| 1958 | unsigned int apic_lvterr; |
| 1959 | unsigned int apic_tmict; |
| 1960 | unsigned int apic_tdcr; |
| 1961 | unsigned int apic_thmr; |
| 1962 | } apic_pm_state; |
| 1963 | |
Pavel Machek | 0b9c33a | 2005-04-16 15:25:31 -0700 | [diff] [blame] | 1964 | static int lapic_suspend(struct sys_device *dev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | { |
| 1966 | unsigned long flags; |
Karsten Wiese | f990fff | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 1967 | int maxlvt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | |
| 1969 | if (!apic_pm_state.active) |
| 1970 | return 0; |
| 1971 | |
Thomas Gleixner | 37e650c | 2008-01-30 13:30:14 +0100 | [diff] [blame] | 1972 | maxlvt = lapic_get_maxlvt(); |
Karsten Wiese | f990fff | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 1973 | |
Suresh Siddha | 2d7a66d | 2008-07-11 14:24:19 -0700 | [diff] [blame] | 1974 | apic_pm_state.apic_id = apic_read(APIC_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI); |
| 1976 | apic_pm_state.apic_ldr = apic_read(APIC_LDR); |
| 1977 | apic_pm_state.apic_dfr = apic_read(APIC_DFR); |
| 1978 | apic_pm_state.apic_spiv = apic_read(APIC_SPIV); |
| 1979 | apic_pm_state.apic_lvtt = apic_read(APIC_LVTT); |
Karsten Wiese | f990fff | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 1980 | if (maxlvt >= 4) |
| 1981 | apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0); |
| 1983 | apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1); |
| 1984 | apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR); |
| 1985 | apic_pm_state.apic_tmict = apic_read(APIC_TMICT); |
| 1986 | apic_pm_state.apic_tdcr = apic_read(APIC_TDCR); |
Cyrill Gorcunov | 24968cf | 2008-08-16 23:21:52 +0400 | [diff] [blame] | 1987 | #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL) |
Karsten Wiese | f990fff | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 1988 | if (maxlvt >= 5) |
| 1989 | apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR); |
| 1990 | #endif |
Cyrill Gorcunov | 24968cf | 2008-08-16 23:21:52 +0400 | [diff] [blame] | 1991 | |
Fernando Luis Vázquez Cao | 2b94ab2 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 1992 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | disable_local_APIC(); |
| 1994 | local_irq_restore(flags); |
| 1995 | return 0; |
| 1996 | } |
| 1997 | |
| 1998 | static int lapic_resume(struct sys_device *dev) |
| 1999 | { |
| 2000 | unsigned int l, h; |
| 2001 | unsigned long flags; |
Karsten Wiese | f990fff | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 2002 | int maxlvt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | |
| 2004 | if (!apic_pm_state.active) |
| 2005 | return 0; |
| 2006 | |
Thomas Gleixner | 37e650c | 2008-01-30 13:30:14 +0100 | [diff] [blame] | 2007 | maxlvt = lapic_get_maxlvt(); |
Karsten Wiese | f990fff | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 2008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | local_irq_save(flags); |
Cyrill Gorcunov | 92206c9 | 2008-08-16 23:21:51 +0400 | [diff] [blame] | 2010 | |
Yinghai Lu | 49899ea | 2008-08-24 02:01:47 -0700 | [diff] [blame] | 2011 | #ifdef HAVE_X2APIC |
Cyrill Gorcunov | 92206c9 | 2008-08-16 23:21:51 +0400 | [diff] [blame] | 2012 | if (x2apic) |
| 2013 | enable_x2apic(); |
| 2014 | else |
| 2015 | #endif |
Yinghai Lu | d5e629a | 2008-08-17 21:12:27 -0700 | [diff] [blame] | 2016 | { |
Cyrill Gorcunov | 92206c9 | 2008-08-16 23:21:51 +0400 | [diff] [blame] | 2017 | /* |
| 2018 | * Make sure the APICBASE points to the right address |
| 2019 | * |
| 2020 | * FIXME! This will be wrong if we ever support suspend on |
| 2021 | * SMP! We'll need to do this as part of the CPU restore! |
| 2022 | */ |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 2023 | rdmsr(MSR_IA32_APICBASE, l, h); |
| 2024 | l &= ~MSR_IA32_APICBASE_BASE; |
| 2025 | l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr; |
| 2026 | wrmsr(MSR_IA32_APICBASE, l, h); |
Yinghai Lu | d5e629a | 2008-08-17 21:12:27 -0700 | [diff] [blame] | 2027 | } |
Suresh Siddha | 6e1cb38 | 2008-07-10 11:16:58 -0700 | [diff] [blame] | 2028 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED); |
| 2030 | apic_write(APIC_ID, apic_pm_state.apic_id); |
| 2031 | apic_write(APIC_DFR, apic_pm_state.apic_dfr); |
| 2032 | apic_write(APIC_LDR, apic_pm_state.apic_ldr); |
| 2033 | apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri); |
| 2034 | apic_write(APIC_SPIV, apic_pm_state.apic_spiv); |
| 2035 | apic_write(APIC_LVT0, apic_pm_state.apic_lvt0); |
| 2036 | apic_write(APIC_LVT1, apic_pm_state.apic_lvt1); |
Cyrill Gorcunov | 92206c9 | 2008-08-16 23:21:51 +0400 | [diff] [blame] | 2037 | #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL) |
Karsten Wiese | f990fff | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 2038 | if (maxlvt >= 5) |
| 2039 | apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr); |
| 2040 | #endif |
| 2041 | if (maxlvt >= 4) |
| 2042 | apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | apic_write(APIC_LVTT, apic_pm_state.apic_lvtt); |
| 2044 | apic_write(APIC_TDCR, apic_pm_state.apic_tdcr); |
| 2045 | apic_write(APIC_TMICT, apic_pm_state.apic_tmict); |
| 2046 | apic_write(APIC_ESR, 0); |
| 2047 | apic_read(APIC_ESR); |
| 2048 | apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr); |
| 2049 | apic_write(APIC_ESR, 0); |
| 2050 | apic_read(APIC_ESR); |
Cyrill Gorcunov | 92206c9 | 2008-08-16 23:21:51 +0400 | [diff] [blame] | 2051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | local_irq_restore(flags); |
Cyrill Gorcunov | 92206c9 | 2008-08-16 23:21:51 +0400 | [diff] [blame] | 2053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | return 0; |
| 2055 | } |
| 2056 | |
Cyrill Gorcunov | 274cfe5 | 2008-08-16 23:21:53 +0400 | [diff] [blame] | 2057 | /* |
| 2058 | * This device has no shutdown method - fully functioning local APICs |
| 2059 | * are needed on every CPU up until machine_halt/restart/poweroff. |
| 2060 | */ |
| 2061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | static struct sysdev_class lapic_sysclass = { |
Kay Sievers | af5ca3f4 | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 2063 | .name = "lapic", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | .resume = lapic_resume, |
| 2065 | .suspend = lapic_suspend, |
| 2066 | }; |
| 2067 | |
| 2068 | static struct sys_device device_lapic = { |
Hiroshi Shimamoto | e83a5fd | 2008-01-30 13:32:35 +0100 | [diff] [blame] | 2069 | .id = 0, |
| 2070 | .cls = &lapic_sysclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | }; |
| 2072 | |
Ashok Raj | e6982c6 | 2005-06-25 14:54:58 -0700 | [diff] [blame] | 2073 | static void __cpuinit apic_pm_activate(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | { |
| 2075 | apic_pm_state.active = 1; |
| 2076 | } |
| 2077 | |
| 2078 | static int __init init_lapic_sysfs(void) |
| 2079 | { |
| 2080 | int error; |
Hiroshi Shimamoto | e83a5fd | 2008-01-30 13:32:35 +0100 | [diff] [blame] | 2081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | if (!cpu_has_apic) |
| 2083 | return 0; |
| 2084 | /* XXX: remove suspend/resume procs if !apic_pm_state.active? */ |
Hiroshi Shimamoto | e83a5fd | 2008-01-30 13:32:35 +0100 | [diff] [blame] | 2085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | error = sysdev_class_register(&lapic_sysclass); |
| 2087 | if (!error) |
| 2088 | error = sysdev_register(&device_lapic); |
| 2089 | return error; |
| 2090 | } |
| 2091 | device_initcall(init_lapic_sysfs); |
| 2092 | |
| 2093 | #else /* CONFIG_PM */ |
| 2094 | |
| 2095 | static void apic_pm_activate(void) { } |
| 2096 | |
| 2097 | #endif /* CONFIG_PM */ |
| 2098 | |
Yinghai Lu | f28c0ae | 2008-08-24 02:01:49 -0700 | [diff] [blame] | 2099 | #ifdef CONFIG_X86_64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | /* |
Vojtech Pavlik | f8bf3c6 | 2006-06-26 13:58:23 +0200 | [diff] [blame] | 2101 | * apic_is_clustered_box() -- Check if we can expect good TSC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | * |
| 2103 | * Thus far, the major user of this is IBM's Summit2 series: |
| 2104 | * |
Linus Torvalds | 637029c | 2006-02-27 20:41:56 -0800 | [diff] [blame] | 2105 | * Clustered boxes may have unsynced TSC problems if they are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | * multi-chassis. Use available data to take a good guess. |
| 2107 | * If in doubt, go HPET. |
| 2108 | */ |
Vojtech Pavlik | f8bf3c6 | 2006-06-26 13:58:23 +0200 | [diff] [blame] | 2109 | __cpuinit int apic_is_clustered_box(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | { |
| 2111 | int i, clusters, zeros; |
| 2112 | unsigned id; |
Yinghai Lu | 322850a | 2008-02-23 21:48:42 -0800 | [diff] [blame] | 2113 | u16 *bios_cpu_apicid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS); |
| 2115 | |
Yinghai Lu | 322850a | 2008-02-23 21:48:42 -0800 | [diff] [blame] | 2116 | /* |
| 2117 | * there is not this kind of box with AMD CPU yet. |
| 2118 | * Some AMD box with quadcore cpu and 8 sockets apicid |
| 2119 | * will be [4, 0x23] or [8, 0x27] could be thought to |
Yinghai Lu | f8fffa4 | 2008-02-24 21:36:28 -0800 | [diff] [blame] | 2120 | * vsmp box still need checking... |
Yinghai Lu | 322850a | 2008-02-23 21:48:42 -0800 | [diff] [blame] | 2121 | */ |
Ravikiran G Thirumalai | 1cb6848 | 2008-03-20 00:45:08 -0700 | [diff] [blame] | 2122 | if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box()) |
Yinghai Lu | 322850a | 2008-02-23 21:48:42 -0800 | [diff] [blame] | 2123 | return 0; |
| 2124 | |
Mike Travis | 23ca4bb | 2008-05-12 21:21:12 +0200 | [diff] [blame] | 2125 | bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid); |
Suresh Siddha | 376ec33f | 2005-05-16 21:53:32 -0700 | [diff] [blame] | 2126 | bitmap_zero(clustermap, NUM_APIC_CLUSTERS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | |
Mike Travis | 168ef54 | 2008-12-16 17:34:01 -0800 | [diff] [blame] | 2128 | for (i = 0; i < nr_cpu_ids; i++) { |
travis@sgi.com | e8c10ef | 2008-01-30 13:33:12 +0100 | [diff] [blame] | 2129 | /* are we being called early in kernel startup? */ |
Mike Travis | 693e3c5 | 2008-01-30 13:33:14 +0100 | [diff] [blame] | 2130 | if (bios_cpu_apicid) { |
| 2131 | id = bios_cpu_apicid[i]; |
Jaswinder Singh Rajput | e423e33 | 2009-01-04 16:16:25 +0530 | [diff] [blame] | 2132 | } else if (i < nr_cpu_ids) { |
travis@sgi.com | e8c10ef | 2008-01-30 13:33:12 +0100 | [diff] [blame] | 2133 | if (cpu_present(i)) |
| 2134 | id = per_cpu(x86_bios_cpu_apicid, i); |
| 2135 | else |
| 2136 | continue; |
Jaswinder Singh Rajput | e423e33 | 2009-01-04 16:16:25 +0530 | [diff] [blame] | 2137 | } else |
travis@sgi.com | e8c10ef | 2008-01-30 13:33:12 +0100 | [diff] [blame] | 2138 | break; |
| 2139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | if (id != BAD_APICID) |
| 2141 | __set_bit(APIC_CLUSTERID(id), clustermap); |
| 2142 | } |
| 2143 | |
| 2144 | /* Problem: Partially populated chassis may not have CPUs in some of |
| 2145 | * the APIC clusters they have been allocated. Only present CPUs have |
travis@sgi.com | 602a54a | 2008-01-30 13:33:21 +0100 | [diff] [blame] | 2146 | * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap. |
| 2147 | * Since clusters are allocated sequentially, count zeros only if |
| 2148 | * they are bounded by ones. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | */ |
| 2150 | clusters = 0; |
| 2151 | zeros = 0; |
| 2152 | for (i = 0; i < NUM_APIC_CLUSTERS; i++) { |
| 2153 | if (test_bit(i, clustermap)) { |
| 2154 | clusters += 1 + zeros; |
| 2155 | zeros = 0; |
| 2156 | } else |
| 2157 | ++zeros; |
| 2158 | } |
| 2159 | |
Ravikiran G Thirumalai | 1cb6848 | 2008-03-20 00:45:08 -0700 | [diff] [blame] | 2160 | /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are |
| 2161 | * not guaranteed to be synced between boards |
| 2162 | */ |
| 2163 | if (is_vsmp_box() && clusters > 1) |
| 2164 | return 1; |
| 2165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | /* |
Vojtech Pavlik | f8bf3c6 | 2006-06-26 13:58:23 +0200 | [diff] [blame] | 2167 | * If clusters > 2, then should be multi-chassis. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | * May have to revisit this when multi-core + hyperthreaded CPUs come |
| 2169 | * out, but AFAIK this will work even for them. |
| 2170 | */ |
| 2171 | return (clusters > 2); |
| 2172 | } |
Yinghai Lu | f28c0ae | 2008-08-24 02:01:49 -0700 | [diff] [blame] | 2173 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | |
| 2175 | /* |
Thomas Gleixner | 0e078e2 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 2176 | * APIC command line parameters |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | */ |
Cyrill Gorcunov | 789fa73 | 2008-08-18 20:46:01 +0400 | [diff] [blame] | 2178 | static int __init setup_disableapic(char *arg) |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 2179 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | disable_apic = 1; |
Yinghai Lu | 9175fc0 | 2008-07-21 01:38:14 -0700 | [diff] [blame] | 2181 | setup_clear_cpu_cap(X86_FEATURE_APIC); |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 2182 | return 0; |
| 2183 | } |
| 2184 | early_param("disableapic", setup_disableapic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 2186 | /* same as disableapic, for compatibility */ |
Cyrill Gorcunov | 789fa73 | 2008-08-18 20:46:01 +0400 | [diff] [blame] | 2187 | static int __init setup_nolapic(char *arg) |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 2188 | { |
Cyrill Gorcunov | 789fa73 | 2008-08-18 20:46:01 +0400 | [diff] [blame] | 2189 | return setup_disableapic(arg); |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 2190 | } |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 2191 | early_param("nolapic", setup_nolapic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | |
Linus Torvalds | 2e7c283 | 2007-03-23 11:32:31 -0700 | [diff] [blame] | 2193 | static int __init parse_lapic_timer_c2_ok(char *arg) |
| 2194 | { |
| 2195 | local_apic_timer_c2_ok = 1; |
| 2196 | return 0; |
| 2197 | } |
| 2198 | early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok); |
| 2199 | |
Cyrill Gorcunov | 36fef09 | 2008-08-15 13:51:20 +0200 | [diff] [blame] | 2200 | static int __init parse_disable_apic_timer(char *arg) |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 2201 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | disable_apic_timer = 1; |
Cyrill Gorcunov | 36fef09 | 2008-08-15 13:51:20 +0200 | [diff] [blame] | 2203 | return 0; |
Thomas Gleixner | 6935d1f | 2007-07-21 17:10:17 +0200 | [diff] [blame] | 2204 | } |
Cyrill Gorcunov | 36fef09 | 2008-08-15 13:51:20 +0200 | [diff] [blame] | 2205 | early_param("noapictimer", parse_disable_apic_timer); |
| 2206 | |
| 2207 | static int __init parse_nolapic_timer(char *arg) |
| 2208 | { |
| 2209 | disable_apic_timer = 1; |
| 2210 | return 0; |
| 2211 | } |
| 2212 | early_param("nolapic_timer", parse_nolapic_timer); |
Andi Kleen | 73dea47 | 2006-02-03 21:50:50 +0100 | [diff] [blame] | 2213 | |
Cyrill Gorcunov | 79af9be | 2008-08-18 20:46:00 +0400 | [diff] [blame] | 2214 | static int __init apic_set_verbosity(char *arg) |
| 2215 | { |
| 2216 | if (!arg) { |
| 2217 | #ifdef CONFIG_X86_64 |
| 2218 | skip_ioapic_setup = 0; |
Cyrill Gorcunov | 79af9be | 2008-08-18 20:46:00 +0400 | [diff] [blame] | 2219 | return 0; |
| 2220 | #endif |
| 2221 | return -EINVAL; |
| 2222 | } |
| 2223 | |
| 2224 | if (strcmp("debug", arg) == 0) |
| 2225 | apic_verbosity = APIC_DEBUG; |
| 2226 | else if (strcmp("verbose", arg) == 0) |
| 2227 | apic_verbosity = APIC_VERBOSE; |
| 2228 | else { |
Cyrill Gorcunov | ba21ebb | 2008-11-10 09:16:41 +0100 | [diff] [blame] | 2229 | pr_warning("APIC Verbosity level %s not recognised" |
Cyrill Gorcunov | 79af9be | 2008-08-18 20:46:00 +0400 | [diff] [blame] | 2230 | " use apic=verbose or apic=debug\n", arg); |
| 2231 | return -EINVAL; |
| 2232 | } |
| 2233 | |
| 2234 | return 0; |
| 2235 | } |
| 2236 | early_param("apic", apic_set_verbosity); |
| 2237 | |
Yinghai Lu | 1e934dd | 2008-02-22 13:37:26 -0800 | [diff] [blame] | 2238 | static int __init lapic_insert_resource(void) |
| 2239 | { |
| 2240 | if (!apic_phys) |
| 2241 | return -1; |
| 2242 | |
| 2243 | /* Put local APIC into the resource map. */ |
| 2244 | lapic_resource.start = apic_phys; |
| 2245 | lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1; |
| 2246 | insert_resource(&iomem_resource, &lapic_resource); |
| 2247 | |
| 2248 | return 0; |
| 2249 | } |
| 2250 | |
| 2251 | /* |
| 2252 | * need call insert after e820_reserve_resources() |
| 2253 | * that is using request_resource |
| 2254 | */ |
| 2255 | late_initcall(lapic_insert_resource); |