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