[PATCH] x86_64: Only switch to IPI broadcast timer on Intel when C3 is supported
[linux-2.6-block.git] / arch / x86_64 / kernel / apic.c
CommitLineData
1da177e4
LT
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
17#include <linux/config.h>
18#include <linux/init.h>
19
20#include <linux/mm.h>
1da177e4
LT
21#include <linux/delay.h>
22#include <linux/bootmem.h>
23#include <linux/smp_lock.h>
24#include <linux/interrupt.h>
25#include <linux/mc146818rtc.h>
26#include <linux/kernel_stat.h>
27#include <linux/sysdev.h>
d25bf7e5 28#include <linux/module.h>
1da177e4
LT
29
30#include <asm/atomic.h>
31#include <asm/smp.h>
32#include <asm/mtrr.h>
33#include <asm/mpspec.h>
34#include <asm/pgalloc.h>
35#include <asm/mach_apic.h>
75152114 36#include <asm/nmi.h>
95833c83 37#include <asm/idle.h>
1da177e4
LT
38
39int apic_verbosity;
40
41int disable_apic_timer __initdata;
42
d25bf7e5
VP
43/*
44 * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
45 * IPIs in place of local APIC timers
46 */
47static cpumask_t timer_interrupt_broadcast_ipi_mask;
48
1da177e4
LT
49/* Using APIC to generate smp_local_timer_interrupt? */
50int using_apic_timer = 0;
51
1da177e4
LT
52static void apic_pm_activate(void);
53
54void enable_NMI_through_LVT0 (void * dummy)
55{
11a8e778 56 unsigned int v;
1da177e4 57
1da177e4 58 v = APIC_DM_NMI; /* unmask and set to NMI */
11a8e778 59 apic_write(APIC_LVT0, v);
1da177e4
LT
60}
61
62int get_maxlvt(void)
63{
11a8e778 64 unsigned int v, maxlvt;
1da177e4
LT
65
66 v = apic_read(APIC_LVR);
1da177e4
LT
67 maxlvt = GET_APIC_MAXLVT(v);
68 return maxlvt;
69}
70
71void clear_local_APIC(void)
72{
73 int maxlvt;
74 unsigned int v;
75
76 maxlvt = get_maxlvt();
77
78 /*
79 * Masking an LVT entry on a P6 can trigger a local APIC error
80 * if the vector is zero. Mask LVTERR first to prevent this.
81 */
82 if (maxlvt >= 3) {
83 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
11a8e778 84 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
1da177e4
LT
85 }
86 /*
87 * Careful: we have to set masks only first to deassert
88 * any level-triggered sources.
89 */
90 v = apic_read(APIC_LVTT);
11a8e778 91 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
1da177e4 92 v = apic_read(APIC_LVT0);
11a8e778 93 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1da177e4 94 v = apic_read(APIC_LVT1);
11a8e778 95 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
1da177e4
LT
96 if (maxlvt >= 4) {
97 v = apic_read(APIC_LVTPC);
11a8e778 98 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
1da177e4
LT
99 }
100
101 /*
102 * Clean APIC state for other OSs:
103 */
11a8e778
AK
104 apic_write(APIC_LVTT, APIC_LVT_MASKED);
105 apic_write(APIC_LVT0, APIC_LVT_MASKED);
106 apic_write(APIC_LVT1, APIC_LVT_MASKED);
1da177e4 107 if (maxlvt >= 3)
11a8e778 108 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
1da177e4 109 if (maxlvt >= 4)
11a8e778 110 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
1da177e4 111 v = GET_APIC_VERSION(apic_read(APIC_LVR));
5a40b7c2
AK
112 apic_write(APIC_ESR, 0);
113 apic_read(APIC_ESR);
1da177e4
LT
114}
115
116void __init connect_bsp_APIC(void)
117{
118 if (pic_mode) {
119 /*
120 * Do not trust the local APIC being empty at bootup.
121 */
122 clear_local_APIC();
123 /*
124 * PIC mode, enable APIC mode in the IMCR, i.e.
125 * connect BSP's local APIC to INT and NMI lines.
126 */
127 apic_printk(APIC_VERBOSE, "leaving PIC mode, enabling APIC mode.\n");
128 outb(0x70, 0x22);
129 outb(0x01, 0x23);
130 }
131}
132
208fb931 133void disconnect_bsp_APIC(int virt_wire_setup)
1da177e4
LT
134{
135 if (pic_mode) {
136 /*
137 * Put the board back into PIC mode (has an effect
138 * only on certain older boards). Note that APIC
139 * interrupts, including IPIs, won't work beyond
140 * this point! The only exception are INIT IPIs.
141 */
142 apic_printk(APIC_QUIET, "disabling APIC mode, entering PIC mode.\n");
143 outb(0x70, 0x22);
144 outb(0x00, 0x23);
145 }
208fb931
EB
146 else {
147 /* Go back to Virtual Wire compatibility mode */
148 unsigned long value;
149
150 /* For the spurious interrupt use vector F, and enable it */
151 value = apic_read(APIC_SPIV);
152 value &= ~APIC_VECTOR_MASK;
153 value |= APIC_SPIV_APIC_ENABLED;
154 value |= 0xf;
11a8e778 155 apic_write(APIC_SPIV, value);
208fb931
EB
156
157 if (!virt_wire_setup) {
158 /* For LVT0 make it edge triggered, active high, external and enabled */
159 value = apic_read(APIC_LVT0);
160 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
161 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
162 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
163 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
164 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
11a8e778 165 apic_write(APIC_LVT0, value);
208fb931
EB
166 }
167 else {
168 /* Disable LVT0 */
11a8e778 169 apic_write(APIC_LVT0, APIC_LVT_MASKED);
208fb931
EB
170 }
171
172 /* For LVT1 make it edge triggered, active high, nmi and enabled */
173 value = apic_read(APIC_LVT1);
174 value &= ~(
175 APIC_MODE_MASK | APIC_SEND_PENDING |
176 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
177 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
178 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
179 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
11a8e778 180 apic_write(APIC_LVT1, value);
208fb931 181 }
1da177e4
LT
182}
183
184void disable_local_APIC(void)
185{
186 unsigned int value;
187
188 clear_local_APIC();
189
190 /*
191 * Disable APIC (implies clearing of registers
192 * for 82489DX!).
193 */
194 value = apic_read(APIC_SPIV);
195 value &= ~APIC_SPIV_APIC_ENABLED;
11a8e778 196 apic_write(APIC_SPIV, value);
1da177e4
LT
197}
198
199/*
200 * This is to verify that we're looking at a real local APIC.
201 * Check these against your board if the CPUs aren't getting
202 * started for no apparent reason.
203 */
204int __init verify_local_APIC(void)
205{
206 unsigned int reg0, reg1;
207
208 /*
209 * The version register is read-only in a real APIC.
210 */
211 reg0 = apic_read(APIC_LVR);
212 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
213 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
214 reg1 = apic_read(APIC_LVR);
215 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
216
217 /*
218 * The two version reads above should print the same
219 * numbers. If the second one is different, then we
220 * poke at a non-APIC.
221 */
222 if (reg1 != reg0)
223 return 0;
224
225 /*
226 * Check if the version looks reasonably.
227 */
228 reg1 = GET_APIC_VERSION(reg0);
229 if (reg1 == 0x00 || reg1 == 0xff)
230 return 0;
231 reg1 = get_maxlvt();
232 if (reg1 < 0x02 || reg1 == 0xff)
233 return 0;
234
235 /*
236 * The ID register is read/write in a real APIC.
237 */
238 reg0 = apic_read(APIC_ID);
239 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
240 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
241 reg1 = apic_read(APIC_ID);
242 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
243 apic_write(APIC_ID, reg0);
244 if (reg1 != (reg0 ^ APIC_ID_MASK))
245 return 0;
246
247 /*
248 * The next two are just to see if we have sane values.
249 * They're only really relevant if we're in Virtual Wire
250 * compatibility mode, but most boxes are anymore.
251 */
252 reg0 = apic_read(APIC_LVT0);
253 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
254 reg1 = apic_read(APIC_LVT1);
255 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
256
257 return 1;
258}
259
260void __init sync_Arb_IDs(void)
261{
262 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
263 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
264 if (ver >= 0x14) /* P4 or higher */
265 return;
266
267 /*
268 * Wait for idle.
269 */
270 apic_wait_icr_idle();
271
272 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
11a8e778 273 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
1da177e4
LT
274 | APIC_DM_INIT);
275}
276
277extern void __error_in_apic_c (void);
278
279/*
280 * An initial setup of the virtual wire mode.
281 */
282void __init init_bsp_APIC(void)
283{
11a8e778 284 unsigned int value;
1da177e4
LT
285
286 /*
287 * Don't do the setup now if we have a SMP BIOS as the
288 * through-I/O-APIC virtual wire mode might be active.
289 */
290 if (smp_found_config || !cpu_has_apic)
291 return;
292
293 value = apic_read(APIC_LVR);
1da177e4
LT
294
295 /*
296 * Do not trust the local APIC being empty at bootup.
297 */
298 clear_local_APIC();
299
300 /*
301 * Enable APIC.
302 */
303 value = apic_read(APIC_SPIV);
304 value &= ~APIC_VECTOR_MASK;
305 value |= APIC_SPIV_APIC_ENABLED;
306 value |= APIC_SPIV_FOCUS_DISABLED;
307 value |= SPURIOUS_APIC_VECTOR;
11a8e778 308 apic_write(APIC_SPIV, value);
1da177e4
LT
309
310 /*
311 * Set up the virtual wire mode.
312 */
11a8e778 313 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1da177e4 314 value = APIC_DM_NMI;
11a8e778 315 apic_write(APIC_LVT1, value);
1da177e4
LT
316}
317
e6982c67 318void __cpuinit setup_local_APIC (void)
1da177e4 319{
11a8e778 320 unsigned int value, maxlvt;
1da177e4 321
1da177e4 322 value = apic_read(APIC_LVR);
1da177e4
LT
323
324 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
325 __error_in_apic_c();
326
327 /*
328 * Double-check whether this APIC is really registered.
329 * This is meaningless in clustered apic mode, so we skip it.
330 */
331 if (!apic_id_registered())
332 BUG();
333
334 /*
335 * Intel recommends to set DFR, LDR and TPR before enabling
336 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
337 * document number 292116). So here it goes...
338 */
339 init_apic_ldr();
340
341 /*
342 * Set Task Priority to 'accept all'. We never change this
343 * later on.
344 */
345 value = apic_read(APIC_TASKPRI);
346 value &= ~APIC_TPRI_MASK;
11a8e778 347 apic_write(APIC_TASKPRI, value);
1da177e4
LT
348
349 /*
350 * Now that we are all set up, enable the APIC
351 */
352 value = apic_read(APIC_SPIV);
353 value &= ~APIC_VECTOR_MASK;
354 /*
355 * Enable APIC
356 */
357 value |= APIC_SPIV_APIC_ENABLED;
358
359 /*
360 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
361 * certain networking cards. If high frequency interrupts are
362 * happening on a particular IOAPIC pin, plus the IOAPIC routing
363 * entry is masked/unmasked at a high rate as well then sooner or
364 * later IOAPIC line gets 'stuck', no more interrupts are received
365 * from the device. If focus CPU is disabled then the hang goes
366 * away, oh well :-(
367 *
368 * [ This bug can be reproduced easily with a level-triggered
369 * PCI Ne2000 networking cards and PII/PIII processors, dual
370 * BX chipset. ]
371 */
372 /*
373 * Actually disabling the focus CPU check just makes the hang less
374 * frequent as it makes the interrupt distributon model be more
375 * like LRU than MRU (the short-term load is more even across CPUs).
376 * See also the comment in end_level_ioapic_irq(). --macro
377 */
378#if 1
379 /* Enable focus processor (bit==0) */
380 value &= ~APIC_SPIV_FOCUS_DISABLED;
381#else
382 /* Disable focus processor (bit==1) */
383 value |= APIC_SPIV_FOCUS_DISABLED;
384#endif
385 /*
386 * Set spurious IRQ vector
387 */
388 value |= SPURIOUS_APIC_VECTOR;
11a8e778 389 apic_write(APIC_SPIV, value);
1da177e4
LT
390
391 /*
392 * Set up LVT0, LVT1:
393 *
394 * set up through-local-APIC on the BP's LINT0. This is not
395 * strictly necessary in pure symmetric-IO mode, but sometimes
396 * we delegate interrupts to the 8259A.
397 */
398 /*
399 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
400 */
401 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
402 if (!smp_processor_id() && (pic_mode || !value)) {
403 value = APIC_DM_EXTINT;
404 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
405 } else {
406 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
407 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
408 }
11a8e778 409 apic_write(APIC_LVT0, value);
1da177e4
LT
410
411 /*
412 * only the BP should see the LINT1 NMI signal, obviously.
413 */
414 if (!smp_processor_id())
415 value = APIC_DM_NMI;
416 else
417 value = APIC_DM_NMI | APIC_LVT_MASKED;
11a8e778 418 apic_write(APIC_LVT1, value);
1da177e4 419
61c11341 420 {
1da177e4
LT
421 unsigned oldvalue;
422 maxlvt = get_maxlvt();
1da177e4
LT
423 oldvalue = apic_read(APIC_ESR);
424 value = ERROR_APIC_VECTOR; // enables sending errors
11a8e778 425 apic_write(APIC_LVTERR, value);
1da177e4
LT
426 /*
427 * spec says clear errors after enabling vector.
428 */
429 if (maxlvt > 3)
430 apic_write(APIC_ESR, 0);
431 value = apic_read(APIC_ESR);
432 if (value != oldvalue)
433 apic_printk(APIC_VERBOSE,
434 "ESR value after enabling vector: %08x, after %08x\n",
435 oldvalue, value);
1da177e4
LT
436 }
437
438 nmi_watchdog_default();
439 if (nmi_watchdog == NMI_LOCAL_APIC)
440 setup_apic_nmi_watchdog();
441 apic_pm_activate();
442}
443
444#ifdef CONFIG_PM
445
446static struct {
447 /* 'active' is true if the local APIC was enabled by us and
448 not the BIOS; this signifies that we are also responsible
449 for disabling it before entering apm/acpi suspend */
450 int active;
451 /* r/w apic fields */
452 unsigned int apic_id;
453 unsigned int apic_taskpri;
454 unsigned int apic_ldr;
455 unsigned int apic_dfr;
456 unsigned int apic_spiv;
457 unsigned int apic_lvtt;
458 unsigned int apic_lvtpc;
459 unsigned int apic_lvt0;
460 unsigned int apic_lvt1;
461 unsigned int apic_lvterr;
462 unsigned int apic_tmict;
463 unsigned int apic_tdcr;
464 unsigned int apic_thmr;
465} apic_pm_state;
466
0b9c33a7 467static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1da177e4
LT
468{
469 unsigned long flags;
470
471 if (!apic_pm_state.active)
472 return 0;
473
474 apic_pm_state.apic_id = apic_read(APIC_ID);
475 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
476 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
477 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
478 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
479 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
480 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
481 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
482 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
483 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
484 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
485 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
486 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
487 local_save_flags(flags);
488 local_irq_disable();
489 disable_local_APIC();
490 local_irq_restore(flags);
491 return 0;
492}
493
494static int lapic_resume(struct sys_device *dev)
495{
496 unsigned int l, h;
497 unsigned long flags;
498
499 if (!apic_pm_state.active)
500 return 0;
501
1da177e4
LT
502 local_irq_save(flags);
503 rdmsr(MSR_IA32_APICBASE, l, h);
504 l &= ~MSR_IA32_APICBASE_BASE;
5b743573 505 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1da177e4
LT
506 wrmsr(MSR_IA32_APICBASE, l, h);
507 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
508 apic_write(APIC_ID, apic_pm_state.apic_id);
509 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
510 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
511 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
512 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
513 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
514 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
515 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
516 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
517 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
518 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
519 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
520 apic_write(APIC_ESR, 0);
521 apic_read(APIC_ESR);
522 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
523 apic_write(APIC_ESR, 0);
524 apic_read(APIC_ESR);
525 local_irq_restore(flags);
526 return 0;
527}
528
529static struct sysdev_class lapic_sysclass = {
530 set_kset_name("lapic"),
531 .resume = lapic_resume,
532 .suspend = lapic_suspend,
533};
534
535static struct sys_device device_lapic = {
536 .id = 0,
537 .cls = &lapic_sysclass,
538};
539
e6982c67 540static void __cpuinit apic_pm_activate(void)
1da177e4
LT
541{
542 apic_pm_state.active = 1;
543}
544
545static int __init init_lapic_sysfs(void)
546{
547 int error;
548 if (!cpu_has_apic)
549 return 0;
550 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
551 error = sysdev_class_register(&lapic_sysclass);
552 if (!error)
553 error = sysdev_register(&device_lapic);
554 return error;
555}
556device_initcall(init_lapic_sysfs);
557
558#else /* CONFIG_PM */
559
560static void apic_pm_activate(void) { }
561
562#endif /* CONFIG_PM */
563
564static int __init apic_set_verbosity(char *str)
565{
566 if (strcmp("debug", str) == 0)
567 apic_verbosity = APIC_DEBUG;
568 else if (strcmp("verbose", str) == 0)
569 apic_verbosity = APIC_VERBOSE;
570 else
571 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
572 " use apic=verbose or apic=debug", str);
573
574 return 0;
575}
576
577__setup("apic=", apic_set_verbosity);
578
579/*
580 * Detect and enable local APICs on non-SMP boards.
581 * Original code written by Keir Fraser.
582 * On AMD64 we trust the BIOS - if it says no APIC it is likely
583 * not correctly set up (usually the APIC timer won't work etc.)
584 */
585
586static int __init detect_init_APIC (void)
587{
588 if (!cpu_has_apic) {
589 printk(KERN_INFO "No local APIC present\n");
590 return -1;
591 }
592
593 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
594 boot_cpu_id = 0;
595 return 0;
596}
597
598void __init init_apic_mappings(void)
599{
600 unsigned long apic_phys;
601
602 /*
603 * If no local APIC can be found then set up a fake all
604 * zeroes page to simulate the local APIC and another
605 * one for the IO-APIC.
606 */
607 if (!smp_found_config && detect_init_APIC()) {
608 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
609 apic_phys = __pa(apic_phys);
610 } else
611 apic_phys = mp_lapic_addr;
612
613 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
614 apic_printk(APIC_VERBOSE,"mapped APIC to %16lx (%16lx)\n", APIC_BASE, apic_phys);
615
616 /*
617 * Fetch the APIC ID of the BSP in case we have a
618 * default configuration (or the MP table is broken).
619 */
1d3fbbf9 620 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
1da177e4
LT
621
622#ifdef CONFIG_X86_IO_APIC
623 {
624 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
625 int i;
626
627 for (i = 0; i < nr_ioapics; i++) {
628 if (smp_found_config) {
629 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
630 } else {
631 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
632 ioapic_phys = __pa(ioapic_phys);
633 }
634 set_fixmap_nocache(idx, ioapic_phys);
635 apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
636 __fix_to_virt(idx), ioapic_phys);
637 idx++;
638 }
639 }
640#endif
641}
642
643/*
644 * This function sets up the local APIC timer, with a timeout of
645 * 'clocks' APIC bus clock. During calibration we actually call
646 * this function twice on the boot CPU, once with a bogus timeout
647 * value, second time for real. The other (noncalibrating) CPUs
648 * call this function only once, with the real, calibrated value.
649 *
650 * We do reads before writes even if unnecessary, to get around the
651 * P5 APIC double write bug.
652 */
653
654#define APIC_DIVISOR 16
655
656static void __setup_APIC_LVTT(unsigned int clocks)
657{
658 unsigned int lvtt_value, tmp_value, ver;
d25bf7e5 659 int cpu = smp_processor_id();
1da177e4
LT
660
661 ver = GET_APIC_VERSION(apic_read(APIC_LVR));
662 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
d25bf7e5
VP
663
664 if (cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask))
665 lvtt_value |= APIC_LVT_MASKED;
666
11a8e778 667 apic_write(APIC_LVTT, lvtt_value);
1da177e4
LT
668
669 /*
670 * Divide PICLK by 16
671 */
672 tmp_value = apic_read(APIC_TDCR);
11a8e778 673 apic_write(APIC_TDCR, (tmp_value
1da177e4
LT
674 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
675 | APIC_TDR_DIV_16);
676
11a8e778 677 apic_write(APIC_TMICT, clocks/APIC_DIVISOR);
1da177e4
LT
678}
679
680static void setup_APIC_timer(unsigned int clocks)
681{
682 unsigned long flags;
683
684 local_irq_save(flags);
685
1da177e4
LT
686 /* wait for irq slice */
687 if (vxtime.hpet_address) {
688 int trigger = hpet_readl(HPET_T0_CMP);
689 while (hpet_readl(HPET_COUNTER) >= trigger)
690 /* do nothing */ ;
691 while (hpet_readl(HPET_COUNTER) < trigger)
692 /* do nothing */ ;
693 } else {
694 int c1, c2;
695 outb_p(0x00, 0x43);
696 c2 = inb_p(0x40);
697 c2 |= inb_p(0x40) << 8;
11a8e778 698 do {
1da177e4
LT
699 c1 = c2;
700 outb_p(0x00, 0x43);
701 c2 = inb_p(0x40);
702 c2 |= inb_p(0x40) << 8;
703 } while (c2 - c1 < 300);
704 }
705
706 __setup_APIC_LVTT(clocks);
707
708 local_irq_restore(flags);
709}
710
711/*
712 * In this function we calibrate APIC bus clocks to the external
713 * timer. Unfortunately we cannot use jiffies and the timer irq
714 * to calibrate, since some later bootup code depends on getting
715 * the first irq? Ugh.
716 *
717 * We want to do the calibration only once since we
718 * want to have local timer irqs syncron. CPUs connected
719 * by the same APIC bus have the very same bus frequency.
720 * And we want to have irqs off anyways, no accidental
721 * APIC irq that way.
722 */
723
724#define TICK_COUNT 100000000
725
726static int __init calibrate_APIC_clock(void)
727{
728 int apic, apic_start, tsc, tsc_start;
729 int result;
730 /*
731 * Put whatever arbitrary (but long enough) timeout
732 * value into the APIC clock, we just want to get the
733 * counter running for calibration.
734 */
735 __setup_APIC_LVTT(1000000000);
736
737 apic_start = apic_read(APIC_TMCCT);
738 rdtscl(tsc_start);
739
740 do {
741 apic = apic_read(APIC_TMCCT);
742 rdtscl(tsc);
743 } while ((tsc - tsc_start) < TICK_COUNT && (apic - apic_start) < TICK_COUNT);
744
745 result = (apic_start - apic) * 1000L * cpu_khz / (tsc - tsc_start);
746
747 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
748 result / 1000 / 1000, result / 1000 % 1000);
749
750 return result * APIC_DIVISOR / HZ;
751}
752
753static unsigned int calibration_result;
754
755void __init setup_boot_APIC_clock (void)
756{
757 if (disable_apic_timer) {
758 printk(KERN_INFO "Disabling APIC timer\n");
759 return;
760 }
761
762 printk(KERN_INFO "Using local APIC timer interrupts.\n");
763 using_apic_timer = 1;
764
765 local_irq_disable();
766
767 calibration_result = calibrate_APIC_clock();
768 /*
769 * Now set up the timer for real.
770 */
771 setup_APIC_timer(calibration_result);
772
773 local_irq_enable();
774}
775
e6982c67 776void __cpuinit setup_secondary_APIC_clock(void)
1da177e4
LT
777{
778 local_irq_disable(); /* FIXME: Do we need this? --RR */
779 setup_APIC_timer(calibration_result);
780 local_irq_enable();
781}
782
d25bf7e5 783void disable_APIC_timer(void)
1da177e4
LT
784{
785 if (using_apic_timer) {
786 unsigned long v;
787
788 v = apic_read(APIC_LVTT);
11a8e778 789 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
1da177e4
LT
790 }
791}
792
793void enable_APIC_timer(void)
794{
d25bf7e5
VP
795 int cpu = smp_processor_id();
796
797 if (using_apic_timer &&
798 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
1da177e4
LT
799 unsigned long v;
800
801 v = apic_read(APIC_LVTT);
11a8e778 802 apic_write(APIC_LVTT, v & ~APIC_LVT_MASKED);
1da177e4
LT
803 }
804}
805
d25bf7e5
VP
806void switch_APIC_timer_to_ipi(void *cpumask)
807{
808 cpumask_t mask = *(cpumask_t *)cpumask;
809 int cpu = smp_processor_id();
810
811 if (cpu_isset(cpu, mask) &&
812 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
813 disable_APIC_timer();
814 cpu_set(cpu, timer_interrupt_broadcast_ipi_mask);
815 }
816}
817EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
818
819void smp_send_timer_broadcast_ipi(void)
820{
821 cpumask_t mask;
822
823 cpus_and(mask, cpu_online_map, timer_interrupt_broadcast_ipi_mask);
824 if (!cpus_empty(mask)) {
825 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
826 }
827}
828
829void switch_ipi_to_APIC_timer(void *cpumask)
830{
831 cpumask_t mask = *(cpumask_t *)cpumask;
832 int cpu = smp_processor_id();
833
834 if (cpu_isset(cpu, mask) &&
835 cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
836 cpu_clear(cpu, timer_interrupt_broadcast_ipi_mask);
837 enable_APIC_timer();
838 }
839}
840EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
841
1da177e4
LT
842int setup_profiling_timer(unsigned int multiplier)
843{
5a07a30c 844 return -EINVAL;
1da177e4
LT
845}
846
89b831ef
JS
847#ifdef CONFIG_X86_MCE_AMD
848void setup_threshold_lvt(unsigned long lvt_off)
849{
850 unsigned int v = 0;
851 unsigned long reg = (lvt_off << 4) + 0x500;
852 v |= THRESHOLD_APIC_VECTOR;
853 apic_write(reg, v);
854}
855#endif /* CONFIG_X86_MCE_AMD */
856
1da177e4
LT
857#undef APIC_DIVISOR
858
859/*
860 * Local timer interrupt handler. It does both profiling and
861 * process statistics/rescheduling.
862 *
863 * We do profiling in every local tick, statistics/rescheduling
864 * happen only every 'profiling multiplier' ticks. The default
865 * multiplier is 1 and it can be changed by writing the new multiplier
866 * value into /proc/profile.
867 */
868
869void smp_local_timer_interrupt(struct pt_regs *regs)
870{
1da177e4 871 profile_tick(CPU_PROFILING, regs);
1da177e4 872#ifdef CONFIG_SMP
5a07a30c 873 update_process_times(user_mode(regs));
1da177e4 874#endif
1da177e4
LT
875 /*
876 * We take the 'long' return path, and there every subsystem
877 * grabs the appropriate locks (kernel lock/ irq lock).
878 *
879 * we might want to decouple profiling from the 'long path',
880 * and do the profiling totally in assembly.
881 *
882 * Currently this isn't too much of an issue (performance wise),
883 * we can take more than 100K local irqs per second on a 100 MHz P5.
884 */
885}
886
887/*
888 * Local APIC timer interrupt. This is the most natural way for doing
889 * local interrupts, but local timer interrupts can be emulated by
890 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
891 *
892 * [ if a single-CPU system runs an SMP kernel then we call the local
893 * interrupt as well. Thus we cannot inline the local irq ... ]
894 */
895void smp_apic_timer_interrupt(struct pt_regs *regs)
896{
897 /*
898 * the NMI deadlock-detector uses this.
899 */
900 add_pda(apic_timer_irqs, 1);
901
902 /*
903 * NOTE! We'd better ACK the irq immediately,
904 * because timer handling can be slow.
905 */
906 ack_APIC_irq();
907 /*
908 * update_process_times() expects us to have done irq_enter().
909 * Besides, if we don't timer interrupts ignore the global
910 * interrupt lock, which is the WrongThing (tm) to do.
911 */
95833c83 912 exit_idle();
1da177e4
LT
913 irq_enter();
914 smp_local_timer_interrupt(regs);
915 irq_exit();
916}
917
918/*
919 * oem_force_hpet_timer -- force HPET mode for some boxes.
920 *
921 * Thus far, the major user of this is IBM's Summit2 series:
922 *
923 * Clustered boxes may have unsynced TSC problems if they are
924 * multi-chassis. Use available data to take a good guess.
925 * If in doubt, go HPET.
926 */
927__init int oem_force_hpet_timer(void)
928{
929 int i, clusters, zeros;
930 unsigned id;
931 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
932
376ec33f 933 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
1da177e4
LT
934
935 for (i = 0; i < NR_CPUS; i++) {
936 id = bios_cpu_apicid[i];
937 if (id != BAD_APICID)
938 __set_bit(APIC_CLUSTERID(id), clustermap);
939 }
940
941 /* Problem: Partially populated chassis may not have CPUs in some of
942 * the APIC clusters they have been allocated. Only present CPUs have
943 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
944 * clusters are allocated sequentially, count zeros only if they are
945 * bounded by ones.
946 */
947 clusters = 0;
948 zeros = 0;
949 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
950 if (test_bit(i, clustermap)) {
951 clusters += 1 + zeros;
952 zeros = 0;
953 } else
954 ++zeros;
955 }
956
957 /*
958 * If clusters > 2, then should be multi-chassis. Return 1 for HPET.
959 * Else return 0 to use TSC.
960 * May have to revisit this when multi-core + hyperthreaded CPUs come
961 * out, but AFAIK this will work even for them.
962 */
963 return (clusters > 2);
964}
965
966/*
967 * This interrupt should _never_ happen with our APIC/SMP architecture
968 */
969asmlinkage void smp_spurious_interrupt(void)
970{
971 unsigned int v;
95833c83 972 exit_idle();
1da177e4
LT
973 irq_enter();
974 /*
975 * Check if this really is a spurious interrupt and ACK it
976 * if it is a vectored one. Just in case...
977 * Spurious interrupts should not be ACKed.
978 */
979 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
980 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
981 ack_APIC_irq();
982
983#if 0
984 static unsigned long last_warning;
985 static unsigned long skipped;
986
987 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
988 if (time_before(last_warning+30*HZ,jiffies)) {
989 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
990 smp_processor_id(), skipped);
991 last_warning = jiffies;
992 skipped = 0;
993 } else {
994 skipped++;
995 }
996#endif
997 irq_exit();
998}
999
1000/*
1001 * This interrupt should never happen with our APIC/SMP architecture
1002 */
1003
1004asmlinkage void smp_error_interrupt(void)
1005{
1006 unsigned int v, v1;
1007
95833c83 1008 exit_idle();
1da177e4
LT
1009 irq_enter();
1010 /* First tickle the hardware, only then report what went on. -- REW */
1011 v = apic_read(APIC_ESR);
1012 apic_write(APIC_ESR, 0);
1013 v1 = apic_read(APIC_ESR);
1014 ack_APIC_irq();
1015 atomic_inc(&irq_err_count);
1016
1017 /* Here is what the APIC error bits mean:
1018 0: Send CS error
1019 1: Receive CS error
1020 2: Send accept error
1021 3: Receive accept error
1022 4: Reserved
1023 5: Send illegal vector
1024 6: Received illegal vector
1025 7: Illegal register address
1026 */
1027 printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1028 smp_processor_id(), v , v1);
1029 irq_exit();
1030}
1031
1032int disable_apic;
1033
1034/*
1035 * This initializes the IO-APIC and APIC hardware if this is
1036 * a UP kernel.
1037 */
1038int __init APIC_init_uniprocessor (void)
1039{
1040 if (disable_apic) {
1041 printk(KERN_INFO "Apic disabled\n");
1042 return -1;
1043 }
1044 if (!cpu_has_apic) {
1045 disable_apic = 1;
1046 printk(KERN_INFO "Apic disabled by BIOS\n");
1047 return -1;
1048 }
1049
1050 verify_local_APIC();
1051
1052 connect_bsp_APIC();
1053
357e11d4 1054 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
11a8e778 1055 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
1da177e4
LT
1056
1057 setup_local_APIC();
1058
1059#ifdef CONFIG_X86_IO_APIC
1060 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1061 setup_IO_APIC();
1062 else
1063 nr_ioapics = 0;
1064#endif
1065 setup_boot_APIC_clock();
75152114 1066 check_nmi_watchdog();
1da177e4
LT
1067 return 0;
1068}
1069
1070static __init int setup_disableapic(char *str)
1071{
1072 disable_apic = 1;
1073 return 0;
1074}
1075
1076static __init int setup_nolapic(char *str)
1077{
1078 disable_apic = 1;
1079 return 0;
1080}
1081
1082static __init int setup_noapictimer(char *str)
1083{
1084 disable_apic_timer = 1;
1085 return 0;
1086}
1087
1088/* dummy parsing: see setup.c */
1089
1090__setup("disableapic", setup_disableapic);
1091__setup("nolapic", setup_nolapic); /* same as disableapic, for compatibility */
1092
1093__setup("noapictimer", setup_noapictimer);
1094
1095/* no "lapic" flag - we only use the lapic when the BIOS tells us so. */