[PATCH] x86_64: x86_64 write apic id fix
[linux-2.6/mini2440.git] / arch / x86_64 / kernel / apic.c
blobc3239f6c22be3fc9b1f72bb7cb5bf9f95338e990
1 /*
2 * Local APIC handling, local APIC timers
4 * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
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.
17 #include <linux/config.h>
18 #include <linux/init.h>
20 #include <linux/mm.h>
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>
28 #include <linux/module.h>
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>
36 #include <asm/nmi.h>
37 #include <asm/idle.h>
39 int apic_verbosity;
41 int disable_apic_timer __initdata;
44 * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
45 * IPIs in place of local APIC timers
47 static cpumask_t timer_interrupt_broadcast_ipi_mask;
49 /* Using APIC to generate smp_local_timer_interrupt? */
50 int using_apic_timer = 0;
52 static void apic_pm_activate(void);
54 void enable_NMI_through_LVT0 (void * dummy)
56 unsigned int v, ver;
58 ver = apic_read(APIC_LVR);
59 ver = GET_APIC_VERSION(ver);
60 v = APIC_DM_NMI; /* unmask and set to NMI */
61 apic_write_around(APIC_LVT0, v);
64 int get_maxlvt(void)
66 unsigned int v, ver, maxlvt;
68 v = apic_read(APIC_LVR);
69 ver = GET_APIC_VERSION(v);
70 maxlvt = GET_APIC_MAXLVT(v);
71 return maxlvt;
74 void clear_local_APIC(void)
76 int maxlvt;
77 unsigned int v;
79 maxlvt = get_maxlvt();
82 * Masking an LVT entry on a P6 can trigger a local APIC error
83 * if the vector is zero. Mask LVTERR first to prevent this.
85 if (maxlvt >= 3) {
86 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
87 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
90 * Careful: we have to set masks only first to deassert
91 * any level-triggered sources.
93 v = apic_read(APIC_LVTT);
94 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
95 v = apic_read(APIC_LVT0);
96 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
97 v = apic_read(APIC_LVT1);
98 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
99 if (maxlvt >= 4) {
100 v = apic_read(APIC_LVTPC);
101 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
105 * Clean APIC state for other OSs:
107 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
108 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
109 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
110 if (maxlvt >= 3)
111 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
112 if (maxlvt >= 4)
113 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
114 v = GET_APIC_VERSION(apic_read(APIC_LVR));
115 apic_write(APIC_ESR, 0);
116 apic_read(APIC_ESR);
119 void __init connect_bsp_APIC(void)
121 if (pic_mode) {
123 * Do not trust the local APIC being empty at bootup.
125 clear_local_APIC();
127 * PIC mode, enable APIC mode in the IMCR, i.e.
128 * connect BSP's local APIC to INT and NMI lines.
130 apic_printk(APIC_VERBOSE, "leaving PIC mode, enabling APIC mode.\n");
131 outb(0x70, 0x22);
132 outb(0x01, 0x23);
136 void disconnect_bsp_APIC(int virt_wire_setup)
138 if (pic_mode) {
140 * Put the board back into PIC mode (has an effect
141 * only on certain older boards). Note that APIC
142 * interrupts, including IPIs, won't work beyond
143 * this point! The only exception are INIT IPIs.
145 apic_printk(APIC_QUIET, "disabling APIC mode, entering PIC mode.\n");
146 outb(0x70, 0x22);
147 outb(0x00, 0x23);
149 else {
150 /* Go back to Virtual Wire compatibility mode */
151 unsigned long value;
153 /* For the spurious interrupt use vector F, and enable it */
154 value = apic_read(APIC_SPIV);
155 value &= ~APIC_VECTOR_MASK;
156 value |= APIC_SPIV_APIC_ENABLED;
157 value |= 0xf;
158 apic_write_around(APIC_SPIV, value);
160 if (!virt_wire_setup) {
161 /* For LVT0 make it edge triggered, active high, external and enabled */
162 value = apic_read(APIC_LVT0);
163 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
164 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
165 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
166 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
167 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
168 apic_write_around(APIC_LVT0, value);
170 else {
171 /* Disable LVT0 */
172 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
175 /* For LVT1 make it edge triggered, active high, nmi and enabled */
176 value = apic_read(APIC_LVT1);
177 value &= ~(
178 APIC_MODE_MASK | APIC_SEND_PENDING |
179 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
180 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
181 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
182 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
183 apic_write_around(APIC_LVT1, value);
187 void disable_local_APIC(void)
189 unsigned int value;
191 clear_local_APIC();
194 * Disable APIC (implies clearing of registers
195 * for 82489DX!).
197 value = apic_read(APIC_SPIV);
198 value &= ~APIC_SPIV_APIC_ENABLED;
199 apic_write_around(APIC_SPIV, value);
203 * This is to verify that we're looking at a real local APIC.
204 * Check these against your board if the CPUs aren't getting
205 * started for no apparent reason.
207 int __init verify_local_APIC(void)
209 unsigned int reg0, reg1;
212 * The version register is read-only in a real APIC.
214 reg0 = apic_read(APIC_LVR);
215 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
216 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
217 reg1 = apic_read(APIC_LVR);
218 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
221 * The two version reads above should print the same
222 * numbers. If the second one is different, then we
223 * poke at a non-APIC.
225 if (reg1 != reg0)
226 return 0;
229 * Check if the version looks reasonably.
231 reg1 = GET_APIC_VERSION(reg0);
232 if (reg1 == 0x00 || reg1 == 0xff)
233 return 0;
234 reg1 = get_maxlvt();
235 if (reg1 < 0x02 || reg1 == 0xff)
236 return 0;
239 * The ID register is read/write in a real APIC.
241 reg0 = apic_read(APIC_ID);
242 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
243 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
244 reg1 = apic_read(APIC_ID);
245 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
246 apic_write(APIC_ID, reg0);
247 if (reg1 != (reg0 ^ APIC_ID_MASK))
248 return 0;
251 * The next two are just to see if we have sane values.
252 * They're only really relevant if we're in Virtual Wire
253 * compatibility mode, but most boxes are anymore.
255 reg0 = apic_read(APIC_LVT0);
256 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
257 reg1 = apic_read(APIC_LVT1);
258 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
260 return 1;
263 void __init sync_Arb_IDs(void)
265 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
266 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
267 if (ver >= 0x14) /* P4 or higher */
268 return;
271 * Wait for idle.
273 apic_wait_icr_idle();
275 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
276 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
277 | APIC_DM_INIT);
280 extern void __error_in_apic_c (void);
283 * An initial setup of the virtual wire mode.
285 void __init init_bsp_APIC(void)
287 unsigned int value, ver;
290 * Don't do the setup now if we have a SMP BIOS as the
291 * through-I/O-APIC virtual wire mode might be active.
293 if (smp_found_config || !cpu_has_apic)
294 return;
296 value = apic_read(APIC_LVR);
297 ver = GET_APIC_VERSION(value);
300 * Do not trust the local APIC being empty at bootup.
302 clear_local_APIC();
305 * Enable APIC.
307 value = apic_read(APIC_SPIV);
308 value &= ~APIC_VECTOR_MASK;
309 value |= APIC_SPIV_APIC_ENABLED;
310 value |= APIC_SPIV_FOCUS_DISABLED;
311 value |= SPURIOUS_APIC_VECTOR;
312 apic_write_around(APIC_SPIV, value);
315 * Set up the virtual wire mode.
317 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
318 value = APIC_DM_NMI;
319 apic_write_around(APIC_LVT1, value);
322 void __cpuinit setup_local_APIC (void)
324 unsigned int value, ver, maxlvt;
326 value = apic_read(APIC_LVR);
327 ver = GET_APIC_VERSION(value);
329 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
330 __error_in_apic_c();
333 * Double-check whether this APIC is really registered.
334 * This is meaningless in clustered apic mode, so we skip it.
336 if (!apic_id_registered())
337 BUG();
340 * Intel recommends to set DFR, LDR and TPR before enabling
341 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
342 * document number 292116). So here it goes...
344 init_apic_ldr();
347 * Set Task Priority to 'accept all'. We never change this
348 * later on.
350 value = apic_read(APIC_TASKPRI);
351 value &= ~APIC_TPRI_MASK;
352 apic_write_around(APIC_TASKPRI, value);
355 * Now that we are all set up, enable the APIC
357 value = apic_read(APIC_SPIV);
358 value &= ~APIC_VECTOR_MASK;
360 * Enable APIC
362 value |= APIC_SPIV_APIC_ENABLED;
365 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
366 * certain networking cards. If high frequency interrupts are
367 * happening on a particular IOAPIC pin, plus the IOAPIC routing
368 * entry is masked/unmasked at a high rate as well then sooner or
369 * later IOAPIC line gets 'stuck', no more interrupts are received
370 * from the device. If focus CPU is disabled then the hang goes
371 * away, oh well :-(
373 * [ This bug can be reproduced easily with a level-triggered
374 * PCI Ne2000 networking cards and PII/PIII processors, dual
375 * BX chipset. ]
378 * Actually disabling the focus CPU check just makes the hang less
379 * frequent as it makes the interrupt distributon model be more
380 * like LRU than MRU (the short-term load is more even across CPUs).
381 * See also the comment in end_level_ioapic_irq(). --macro
383 #if 1
384 /* Enable focus processor (bit==0) */
385 value &= ~APIC_SPIV_FOCUS_DISABLED;
386 #else
387 /* Disable focus processor (bit==1) */
388 value |= APIC_SPIV_FOCUS_DISABLED;
389 #endif
391 * Set spurious IRQ vector
393 value |= SPURIOUS_APIC_VECTOR;
394 apic_write_around(APIC_SPIV, value);
397 * Set up LVT0, LVT1:
399 * set up through-local-APIC on the BP's LINT0. This is not
400 * strictly necessary in pure symmetric-IO mode, but sometimes
401 * we delegate interrupts to the 8259A.
404 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
406 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
407 if (!smp_processor_id() && (pic_mode || !value)) {
408 value = APIC_DM_EXTINT;
409 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
410 } else {
411 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
412 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
414 apic_write_around(APIC_LVT0, value);
417 * only the BP should see the LINT1 NMI signal, obviously.
419 if (!smp_processor_id())
420 value = APIC_DM_NMI;
421 else
422 value = APIC_DM_NMI | APIC_LVT_MASKED;
423 apic_write_around(APIC_LVT1, value);
426 unsigned oldvalue;
427 maxlvt = get_maxlvt();
428 oldvalue = apic_read(APIC_ESR);
429 value = ERROR_APIC_VECTOR; // enables sending errors
430 apic_write_around(APIC_LVTERR, value);
432 * spec says clear errors after enabling vector.
434 if (maxlvt > 3)
435 apic_write(APIC_ESR, 0);
436 value = apic_read(APIC_ESR);
437 if (value != oldvalue)
438 apic_printk(APIC_VERBOSE,
439 "ESR value after enabling vector: %08x, after %08x\n",
440 oldvalue, value);
443 nmi_watchdog_default();
444 if (nmi_watchdog == NMI_LOCAL_APIC)
445 setup_apic_nmi_watchdog();
446 apic_pm_activate();
449 #ifdef CONFIG_PM
451 static struct {
452 /* 'active' is true if the local APIC was enabled by us and
453 not the BIOS; this signifies that we are also responsible
454 for disabling it before entering apm/acpi suspend */
455 int active;
456 /* r/w apic fields */
457 unsigned int apic_id;
458 unsigned int apic_taskpri;
459 unsigned int apic_ldr;
460 unsigned int apic_dfr;
461 unsigned int apic_spiv;
462 unsigned int apic_lvtt;
463 unsigned int apic_lvtpc;
464 unsigned int apic_lvt0;
465 unsigned int apic_lvt1;
466 unsigned int apic_lvterr;
467 unsigned int apic_tmict;
468 unsigned int apic_tdcr;
469 unsigned int apic_thmr;
470 } apic_pm_state;
472 static int lapic_suspend(struct sys_device *dev, pm_message_t state)
474 unsigned long flags;
476 if (!apic_pm_state.active)
477 return 0;
479 apic_pm_state.apic_id = apic_read(APIC_ID);
480 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
481 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
482 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
483 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
484 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
485 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
486 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
487 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
488 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
489 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
490 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
491 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
492 local_save_flags(flags);
493 local_irq_disable();
494 disable_local_APIC();
495 local_irq_restore(flags);
496 return 0;
499 static int lapic_resume(struct sys_device *dev)
501 unsigned int l, h;
502 unsigned long flags;
504 if (!apic_pm_state.active)
505 return 0;
507 /* XXX: Pavel needs this for S3 resume, but can't explain why */
508 set_fixmap_nocache(FIX_APIC_BASE, APIC_DEFAULT_PHYS_BASE);
510 local_irq_save(flags);
511 rdmsr(MSR_IA32_APICBASE, l, h);
512 l &= ~MSR_IA32_APICBASE_BASE;
513 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
514 wrmsr(MSR_IA32_APICBASE, l, h);
515 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
516 apic_write(APIC_ID, apic_pm_state.apic_id);
517 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
518 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
519 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
520 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
521 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
522 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
523 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
524 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
525 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
526 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
527 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
528 apic_write(APIC_ESR, 0);
529 apic_read(APIC_ESR);
530 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
531 apic_write(APIC_ESR, 0);
532 apic_read(APIC_ESR);
533 local_irq_restore(flags);
534 return 0;
537 static struct sysdev_class lapic_sysclass = {
538 set_kset_name("lapic"),
539 .resume = lapic_resume,
540 .suspend = lapic_suspend,
543 static struct sys_device device_lapic = {
544 .id = 0,
545 .cls = &lapic_sysclass,
548 static void __cpuinit apic_pm_activate(void)
550 apic_pm_state.active = 1;
553 static int __init init_lapic_sysfs(void)
555 int error;
556 if (!cpu_has_apic)
557 return 0;
558 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
559 error = sysdev_class_register(&lapic_sysclass);
560 if (!error)
561 error = sysdev_register(&device_lapic);
562 return error;
564 device_initcall(init_lapic_sysfs);
566 #else /* CONFIG_PM */
568 static void apic_pm_activate(void) { }
570 #endif /* CONFIG_PM */
572 static int __init apic_set_verbosity(char *str)
574 if (strcmp("debug", str) == 0)
575 apic_verbosity = APIC_DEBUG;
576 else if (strcmp("verbose", str) == 0)
577 apic_verbosity = APIC_VERBOSE;
578 else
579 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
580 " use apic=verbose or apic=debug", str);
582 return 0;
585 __setup("apic=", apic_set_verbosity);
588 * Detect and enable local APICs on non-SMP boards.
589 * Original code written by Keir Fraser.
590 * On AMD64 we trust the BIOS - if it says no APIC it is likely
591 * not correctly set up (usually the APIC timer won't work etc.)
594 static int __init detect_init_APIC (void)
596 if (!cpu_has_apic) {
597 printk(KERN_INFO "No local APIC present\n");
598 return -1;
601 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
602 boot_cpu_id = 0;
603 return 0;
606 void __init init_apic_mappings(void)
608 unsigned long apic_phys;
611 * If no local APIC can be found then set up a fake all
612 * zeroes page to simulate the local APIC and another
613 * one for the IO-APIC.
615 if (!smp_found_config && detect_init_APIC()) {
616 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
617 apic_phys = __pa(apic_phys);
618 } else
619 apic_phys = mp_lapic_addr;
621 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
622 apic_printk(APIC_VERBOSE,"mapped APIC to %16lx (%16lx)\n", APIC_BASE, apic_phys);
625 * Fetch the APIC ID of the BSP in case we have a
626 * default configuration (or the MP table is broken).
628 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
630 #ifdef CONFIG_X86_IO_APIC
632 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
633 int i;
635 for (i = 0; i < nr_ioapics; i++) {
636 if (smp_found_config) {
637 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
638 } else {
639 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
640 ioapic_phys = __pa(ioapic_phys);
642 set_fixmap_nocache(idx, ioapic_phys);
643 apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
644 __fix_to_virt(idx), ioapic_phys);
645 idx++;
648 #endif
652 * This function sets up the local APIC timer, with a timeout of
653 * 'clocks' APIC bus clock. During calibration we actually call
654 * this function twice on the boot CPU, once with a bogus timeout
655 * value, second time for real. The other (noncalibrating) CPUs
656 * call this function only once, with the real, calibrated value.
658 * We do reads before writes even if unnecessary, to get around the
659 * P5 APIC double write bug.
662 #define APIC_DIVISOR 16
664 static void __setup_APIC_LVTT(unsigned int clocks)
666 unsigned int lvtt_value, tmp_value, ver;
667 int cpu = smp_processor_id();
669 ver = GET_APIC_VERSION(apic_read(APIC_LVR));
670 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
672 if (cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask))
673 lvtt_value |= APIC_LVT_MASKED;
675 apic_write_around(APIC_LVTT, lvtt_value);
678 * Divide PICLK by 16
680 tmp_value = apic_read(APIC_TDCR);
681 apic_write_around(APIC_TDCR, (tmp_value
682 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
683 | APIC_TDR_DIV_16);
685 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
688 static void setup_APIC_timer(unsigned int clocks)
690 unsigned long flags;
692 local_irq_save(flags);
694 /* For some reasons this doesn't work on Simics, so fake it for now */
695 if (!strstr(boot_cpu_data.x86_model_id, "Screwdriver")) {
696 __setup_APIC_LVTT(clocks);
697 return;
700 /* wait for irq slice */
701 if (vxtime.hpet_address) {
702 int trigger = hpet_readl(HPET_T0_CMP);
703 while (hpet_readl(HPET_COUNTER) >= trigger)
704 /* do nothing */ ;
705 while (hpet_readl(HPET_COUNTER) < trigger)
706 /* do nothing */ ;
707 } else {
708 int c1, c2;
709 outb_p(0x00, 0x43);
710 c2 = inb_p(0x40);
711 c2 |= inb_p(0x40) << 8;
712 do {
713 c1 = c2;
714 outb_p(0x00, 0x43);
715 c2 = inb_p(0x40);
716 c2 |= inb_p(0x40) << 8;
717 } while (c2 - c1 < 300);
720 __setup_APIC_LVTT(clocks);
722 local_irq_restore(flags);
726 * In this function we calibrate APIC bus clocks to the external
727 * timer. Unfortunately we cannot use jiffies and the timer irq
728 * to calibrate, since some later bootup code depends on getting
729 * the first irq? Ugh.
731 * We want to do the calibration only once since we
732 * want to have local timer irqs syncron. CPUs connected
733 * by the same APIC bus have the very same bus frequency.
734 * And we want to have irqs off anyways, no accidental
735 * APIC irq that way.
738 #define TICK_COUNT 100000000
740 static int __init calibrate_APIC_clock(void)
742 int apic, apic_start, tsc, tsc_start;
743 int result;
745 * Put whatever arbitrary (but long enough) timeout
746 * value into the APIC clock, we just want to get the
747 * counter running for calibration.
749 __setup_APIC_LVTT(1000000000);
751 apic_start = apic_read(APIC_TMCCT);
752 rdtscl(tsc_start);
754 do {
755 apic = apic_read(APIC_TMCCT);
756 rdtscl(tsc);
757 } while ((tsc - tsc_start) < TICK_COUNT && (apic - apic_start) < TICK_COUNT);
759 result = (apic_start - apic) * 1000L * cpu_khz / (tsc - tsc_start);
761 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
762 result / 1000 / 1000, result / 1000 % 1000);
764 return result * APIC_DIVISOR / HZ;
767 static unsigned int calibration_result;
769 void __init setup_boot_APIC_clock (void)
771 if (disable_apic_timer) {
772 printk(KERN_INFO "Disabling APIC timer\n");
773 return;
776 printk(KERN_INFO "Using local APIC timer interrupts.\n");
777 using_apic_timer = 1;
779 local_irq_disable();
781 calibration_result = calibrate_APIC_clock();
783 * Now set up the timer for real.
785 setup_APIC_timer(calibration_result);
787 local_irq_enable();
790 void __cpuinit setup_secondary_APIC_clock(void)
792 local_irq_disable(); /* FIXME: Do we need this? --RR */
793 setup_APIC_timer(calibration_result);
794 local_irq_enable();
797 void disable_APIC_timer(void)
799 if (using_apic_timer) {
800 unsigned long v;
802 v = apic_read(APIC_LVTT);
803 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
807 void enable_APIC_timer(void)
809 int cpu = smp_processor_id();
811 if (using_apic_timer &&
812 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
813 unsigned long v;
815 v = apic_read(APIC_LVTT);
816 apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
820 void switch_APIC_timer_to_ipi(void *cpumask)
822 cpumask_t mask = *(cpumask_t *)cpumask;
823 int cpu = smp_processor_id();
825 if (cpu_isset(cpu, mask) &&
826 !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
827 disable_APIC_timer();
828 cpu_set(cpu, timer_interrupt_broadcast_ipi_mask);
831 EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
833 void smp_send_timer_broadcast_ipi(void)
835 cpumask_t mask;
837 cpus_and(mask, cpu_online_map, timer_interrupt_broadcast_ipi_mask);
838 if (!cpus_empty(mask)) {
839 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
843 void switch_ipi_to_APIC_timer(void *cpumask)
845 cpumask_t mask = *(cpumask_t *)cpumask;
846 int cpu = smp_processor_id();
848 if (cpu_isset(cpu, mask) &&
849 cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
850 cpu_clear(cpu, timer_interrupt_broadcast_ipi_mask);
851 enable_APIC_timer();
854 EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
856 int setup_profiling_timer(unsigned int multiplier)
858 return -EINVAL;
861 #ifdef CONFIG_X86_MCE_AMD
862 void setup_threshold_lvt(unsigned long lvt_off)
864 unsigned int v = 0;
865 unsigned long reg = (lvt_off << 4) + 0x500;
866 v |= THRESHOLD_APIC_VECTOR;
867 apic_write(reg, v);
869 #endif /* CONFIG_X86_MCE_AMD */
871 #undef APIC_DIVISOR
874 * Local timer interrupt handler. It does both profiling and
875 * process statistics/rescheduling.
877 * We do profiling in every local tick, statistics/rescheduling
878 * happen only every 'profiling multiplier' ticks. The default
879 * multiplier is 1 and it can be changed by writing the new multiplier
880 * value into /proc/profile.
883 void smp_local_timer_interrupt(struct pt_regs *regs)
885 profile_tick(CPU_PROFILING, regs);
886 #ifdef CONFIG_SMP
887 update_process_times(user_mode(regs));
888 #endif
890 * We take the 'long' return path, and there every subsystem
891 * grabs the appropriate locks (kernel lock/ irq lock).
893 * we might want to decouple profiling from the 'long path',
894 * and do the profiling totally in assembly.
896 * Currently this isn't too much of an issue (performance wise),
897 * we can take more than 100K local irqs per second on a 100 MHz P5.
902 * Local APIC timer interrupt. This is the most natural way for doing
903 * local interrupts, but local timer interrupts can be emulated by
904 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
906 * [ if a single-CPU system runs an SMP kernel then we call the local
907 * interrupt as well. Thus we cannot inline the local irq ... ]
909 void smp_apic_timer_interrupt(struct pt_regs *regs)
912 * the NMI deadlock-detector uses this.
914 add_pda(apic_timer_irqs, 1);
917 * NOTE! We'd better ACK the irq immediately,
918 * because timer handling can be slow.
920 ack_APIC_irq();
922 * update_process_times() expects us to have done irq_enter().
923 * Besides, if we don't timer interrupts ignore the global
924 * interrupt lock, which is the WrongThing (tm) to do.
926 exit_idle();
927 irq_enter();
928 smp_local_timer_interrupt(regs);
929 irq_exit();
933 * oem_force_hpet_timer -- force HPET mode for some boxes.
935 * Thus far, the major user of this is IBM's Summit2 series:
937 * Clustered boxes may have unsynced TSC problems if they are
938 * multi-chassis. Use available data to take a good guess.
939 * If in doubt, go HPET.
941 __init int oem_force_hpet_timer(void)
943 int i, clusters, zeros;
944 unsigned id;
945 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
947 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
949 for (i = 0; i < NR_CPUS; i++) {
950 id = bios_cpu_apicid[i];
951 if (id != BAD_APICID)
952 __set_bit(APIC_CLUSTERID(id), clustermap);
955 /* Problem: Partially populated chassis may not have CPUs in some of
956 * the APIC clusters they have been allocated. Only present CPUs have
957 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
958 * clusters are allocated sequentially, count zeros only if they are
959 * bounded by ones.
961 clusters = 0;
962 zeros = 0;
963 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
964 if (test_bit(i, clustermap)) {
965 clusters += 1 + zeros;
966 zeros = 0;
967 } else
968 ++zeros;
972 * If clusters > 2, then should be multi-chassis. Return 1 for HPET.
973 * Else return 0 to use TSC.
974 * May have to revisit this when multi-core + hyperthreaded CPUs come
975 * out, but AFAIK this will work even for them.
977 return (clusters > 2);
981 * This interrupt should _never_ happen with our APIC/SMP architecture
983 asmlinkage void smp_spurious_interrupt(void)
985 unsigned int v;
986 exit_idle();
987 irq_enter();
989 * Check if this really is a spurious interrupt and ACK it
990 * if it is a vectored one. Just in case...
991 * Spurious interrupts should not be ACKed.
993 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
994 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
995 ack_APIC_irq();
997 #if 0
998 static unsigned long last_warning;
999 static unsigned long skipped;
1001 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1002 if (time_before(last_warning+30*HZ,jiffies)) {
1003 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
1004 smp_processor_id(), skipped);
1005 last_warning = jiffies;
1006 skipped = 0;
1007 } else {
1008 skipped++;
1010 #endif
1011 irq_exit();
1015 * This interrupt should never happen with our APIC/SMP architecture
1018 asmlinkage void smp_error_interrupt(void)
1020 unsigned int v, v1;
1022 exit_idle();
1023 irq_enter();
1024 /* First tickle the hardware, only then report what went on. -- REW */
1025 v = apic_read(APIC_ESR);
1026 apic_write(APIC_ESR, 0);
1027 v1 = apic_read(APIC_ESR);
1028 ack_APIC_irq();
1029 atomic_inc(&irq_err_count);
1031 /* Here is what the APIC error bits mean:
1032 0: Send CS error
1033 1: Receive CS error
1034 2: Send accept error
1035 3: Receive accept error
1036 4: Reserved
1037 5: Send illegal vector
1038 6: Received illegal vector
1039 7: Illegal register address
1041 printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1042 smp_processor_id(), v , v1);
1043 irq_exit();
1046 int disable_apic;
1049 * This initializes the IO-APIC and APIC hardware if this is
1050 * a UP kernel.
1052 int __init APIC_init_uniprocessor (void)
1054 if (disable_apic) {
1055 printk(KERN_INFO "Apic disabled\n");
1056 return -1;
1058 if (!cpu_has_apic) {
1059 disable_apic = 1;
1060 printk(KERN_INFO "Apic disabled by BIOS\n");
1061 return -1;
1064 verify_local_APIC();
1066 connect_bsp_APIC();
1068 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
1069 apic_write_around(APIC_ID, SET_APIC_ID(boot_cpu_id));
1071 setup_local_APIC();
1073 #ifdef CONFIG_X86_IO_APIC
1074 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1075 setup_IO_APIC();
1076 else
1077 nr_ioapics = 0;
1078 #endif
1079 setup_boot_APIC_clock();
1080 check_nmi_watchdog();
1081 return 0;
1084 static __init int setup_disableapic(char *str)
1086 disable_apic = 1;
1087 return 0;
1090 static __init int setup_nolapic(char *str)
1092 disable_apic = 1;
1093 return 0;
1096 static __init int setup_noapictimer(char *str)
1098 disable_apic_timer = 1;
1099 return 0;
1102 /* dummy parsing: see setup.c */
1104 __setup("disableapic", setup_disableapic);
1105 __setup("nolapic", setup_nolapic); /* same as disableapic, for compatibility */
1107 __setup("noapictimer", setup_noapictimer);
1109 /* no "lapic" flag - we only use the lapic when the BIOS tells us so. */