MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / arch / x86_64 / kernel / apic.c
blobddd8aac2a03da94c118f5200972dfe5e9be04b41
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/irq.h>
22 #include <linux/delay.h>
23 #include <linux/bootmem.h>
24 #include <linux/smp_lock.h>
25 #include <linux/interrupt.h>
26 #include <linux/mc146818rtc.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/sysdev.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>
36 int apic_verbosity;
38 int disable_apic_timer __initdata;
40 /* Using APIC to generate smp_local_timer_interrupt? */
41 int using_apic_timer = 0;
43 static DEFINE_PER_CPU(int, prof_multiplier) = 1;
44 static DEFINE_PER_CPU(int, prof_old_multiplier) = 1;
45 static DEFINE_PER_CPU(int, prof_counter) = 1;
47 static void apic_pm_activate(void);
49 void enable_NMI_through_LVT0 (void * dummy)
51 unsigned int v, ver;
53 ver = apic_read(APIC_LVR);
54 ver = GET_APIC_VERSION(ver);
55 v = APIC_DM_NMI; /* unmask and set to NMI */
56 apic_write_around(APIC_LVT0, v);
59 int get_maxlvt(void)
61 unsigned int v, ver, maxlvt;
63 v = apic_read(APIC_LVR);
64 ver = GET_APIC_VERSION(v);
65 maxlvt = GET_APIC_MAXLVT(v);
66 return maxlvt;
69 void clear_local_APIC(void)
71 int maxlvt;
72 unsigned int v;
74 maxlvt = get_maxlvt();
77 * Masking an LVT entry on a P6 can trigger a local APIC error
78 * if the vector is zero. Mask LVTERR first to prevent this.
80 if (maxlvt >= 3) {
81 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
82 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
85 * Careful: we have to set masks only first to deassert
86 * any level-triggered sources.
88 v = apic_read(APIC_LVTT);
89 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
90 v = apic_read(APIC_LVT0);
91 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
92 v = apic_read(APIC_LVT1);
93 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
94 if (maxlvt >= 4) {
95 v = apic_read(APIC_LVTPC);
96 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
100 * Clean APIC state for other OSs:
102 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
103 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
104 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
105 if (maxlvt >= 3)
106 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
107 if (maxlvt >= 4)
108 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
109 v = GET_APIC_VERSION(apic_read(APIC_LVR));
110 if (APIC_INTEGRATED(v)) { /* !82489DX */
111 if (maxlvt > 3) /* Due to Pentium errata 3AP and 11AP. */
112 apic_write(APIC_ESR, 0);
113 apic_read(APIC_ESR);
117 void __init connect_bsp_APIC(void)
119 if (pic_mode) {
121 * Do not trust the local APIC being empty at bootup.
123 clear_local_APIC();
125 * PIC mode, enable APIC mode in the IMCR, i.e.
126 * connect BSP's local APIC to INT and NMI lines.
128 apic_printk(APIC_VERBOSE, "leaving PIC mode, enabling APIC mode.\n");
129 outb(0x70, 0x22);
130 outb(0x01, 0x23);
134 void disconnect_bsp_APIC(void)
136 if (pic_mode) {
138 * Put the board back into PIC mode (has an effect
139 * only on certain older boards). Note that APIC
140 * interrupts, including IPIs, won't work beyond
141 * this point! The only exception are INIT IPIs.
143 apic_printk(APIC_QUIET, "disabling APIC mode, entering PIC mode.\n");
144 outb(0x70, 0x22);
145 outb(0x00, 0x23);
149 void disable_local_APIC(void)
151 unsigned int value;
153 clear_local_APIC();
156 * Disable APIC (implies clearing of registers
157 * for 82489DX!).
159 value = apic_read(APIC_SPIV);
160 value &= ~APIC_SPIV_APIC_ENABLED;
161 apic_write_around(APIC_SPIV, value);
165 * This is to verify that we're looking at a real local APIC.
166 * Check these against your board if the CPUs aren't getting
167 * started for no apparent reason.
169 int __init verify_local_APIC(void)
171 unsigned int reg0, reg1;
174 * The version register is read-only in a real APIC.
176 reg0 = apic_read(APIC_LVR);
177 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
178 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
179 reg1 = apic_read(APIC_LVR);
180 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
183 * The two version reads above should print the same
184 * numbers. If the second one is different, then we
185 * poke at a non-APIC.
187 if (reg1 != reg0)
188 return 0;
191 * Check if the version looks reasonably.
193 reg1 = GET_APIC_VERSION(reg0);
194 if (reg1 == 0x00 || reg1 == 0xff)
195 return 0;
196 reg1 = get_maxlvt();
197 if (reg1 < 0x02 || reg1 == 0xff)
198 return 0;
201 * The ID register is read/write in a real APIC.
203 reg0 = apic_read(APIC_ID);
204 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
205 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
206 reg1 = apic_read(APIC_ID);
207 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
208 apic_write(APIC_ID, reg0);
209 if (reg1 != (reg0 ^ APIC_ID_MASK))
210 return 0;
213 * The next two are just to see if we have sane values.
214 * They're only really relevant if we're in Virtual Wire
215 * compatibility mode, but most boxes are anymore.
217 reg0 = apic_read(APIC_LVT0);
218 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
219 reg1 = apic_read(APIC_LVT1);
220 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
222 return 1;
225 void __init sync_Arb_IDs(void)
228 * Wait for idle.
230 apic_wait_icr_idle();
232 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
233 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
234 | APIC_DM_INIT);
237 extern void __error_in_apic_c (void);
240 * An initial setup of the virtual wire mode.
242 void __init init_bsp_APIC(void)
244 unsigned int value, ver;
247 * Don't do the setup now if we have a SMP BIOS as the
248 * through-I/O-APIC virtual wire mode might be active.
250 if (smp_found_config || !cpu_has_apic)
251 return;
253 value = apic_read(APIC_LVR);
254 ver = GET_APIC_VERSION(value);
257 * Do not trust the local APIC being empty at bootup.
259 clear_local_APIC();
262 * Enable APIC.
264 value = apic_read(APIC_SPIV);
265 value &= ~APIC_VECTOR_MASK;
266 value |= APIC_SPIV_APIC_ENABLED;
267 value |= APIC_SPIV_FOCUS_DISABLED;
268 value |= SPURIOUS_APIC_VECTOR;
269 apic_write_around(APIC_SPIV, value);
272 * Set up the virtual wire mode.
274 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
275 value = APIC_DM_NMI;
276 if (!APIC_INTEGRATED(ver)) /* 82489DX */
277 value |= APIC_LVT_LEVEL_TRIGGER;
278 apic_write_around(APIC_LVT1, value);
281 void __init setup_local_APIC (void)
283 unsigned int value, ver, maxlvt;
285 /* Pound the ESR really hard over the head with a big hammer - mbligh */
286 if (esr_disable) {
287 apic_write(APIC_ESR, 0);
288 apic_write(APIC_ESR, 0);
289 apic_write(APIC_ESR, 0);
290 apic_write(APIC_ESR, 0);
293 value = apic_read(APIC_LVR);
294 ver = GET_APIC_VERSION(value);
296 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
297 __error_in_apic_c();
300 * Double-check whether this APIC is really registered.
301 * This is meaningless in clustered apic mode, so we skip it.
303 if (!clustered_apic_mode &&
304 !physid_isset(GET_APIC_ID(apic_read(APIC_ID)), phys_cpu_present_map))
305 BUG();
308 * Intel recommends to set DFR, LDR and TPR before enabling
309 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
310 * document number 292116). So here it goes...
313 if (!clustered_apic_mode) {
315 * In clustered apic mode, the firmware does this for us
316 * Put the APIC into flat delivery mode.
317 * Must be "all ones" explicitly for 82489DX.
319 apic_write_around(APIC_DFR, 0xffffffff);
322 * Set up the logical destination ID.
324 value = apic_read(APIC_LDR);
325 value &= ~APIC_LDR_MASK;
326 value |= (1<<(smp_processor_id()+24));
327 apic_write_around(APIC_LDR, value);
331 * Set Task Priority to 'accept all'. We never change this
332 * later on.
334 value = apic_read(APIC_TASKPRI);
335 value &= ~APIC_TPRI_MASK;
336 apic_write_around(APIC_TASKPRI, value);
339 * Now that we are all set up, enable the APIC
341 value = apic_read(APIC_SPIV);
342 value &= ~APIC_VECTOR_MASK;
344 * Enable APIC
346 value |= APIC_SPIV_APIC_ENABLED;
349 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
350 * certain networking cards. If high frequency interrupts are
351 * happening on a particular IOAPIC pin, plus the IOAPIC routing
352 * entry is masked/unmasked at a high rate as well then sooner or
353 * later IOAPIC line gets 'stuck', no more interrupts are received
354 * from the device. If focus CPU is disabled then the hang goes
355 * away, oh well :-(
357 * [ This bug can be reproduced easily with a level-triggered
358 * PCI Ne2000 networking cards and PII/PIII processors, dual
359 * BX chipset. ]
362 * Actually disabling the focus CPU check just makes the hang less
363 * frequent as it makes the interrupt distributon model be more
364 * like LRU than MRU (the short-term load is more even across CPUs).
365 * See also the comment in end_level_ioapic_irq(). --macro
367 #if 1
368 /* Enable focus processor (bit==0) */
369 value &= ~APIC_SPIV_FOCUS_DISABLED;
370 #else
371 /* Disable focus processor (bit==1) */
372 value |= APIC_SPIV_FOCUS_DISABLED;
373 #endif
375 * Set spurious IRQ vector
377 value |= SPURIOUS_APIC_VECTOR;
378 apic_write_around(APIC_SPIV, value);
381 * Set up LVT0, LVT1:
383 * set up through-local-APIC on the BP's LINT0. This is not
384 * strictly necessary in pure symmetric-IO mode, but sometimes
385 * we delegate interrupts to the 8259A.
388 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
390 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
391 if (!smp_processor_id() && (pic_mode || !value)) {
392 value = APIC_DM_EXTINT;
393 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
394 } else {
395 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
396 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
398 apic_write_around(APIC_LVT0, value);
401 * only the BP should see the LINT1 NMI signal, obviously.
403 if (!smp_processor_id())
404 value = APIC_DM_NMI;
405 else
406 value = APIC_DM_NMI | APIC_LVT_MASKED;
407 if (!APIC_INTEGRATED(ver)) /* 82489DX */
408 value |= APIC_LVT_LEVEL_TRIGGER;
409 apic_write_around(APIC_LVT1, value);
411 if (APIC_INTEGRATED(ver) && !esr_disable) { /* !82489DX */
412 unsigned oldvalue;
413 maxlvt = get_maxlvt();
414 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
415 apic_write(APIC_ESR, 0);
416 oldvalue = apic_read(APIC_ESR);
417 value = ERROR_APIC_VECTOR; // enables sending errors
418 apic_write_around(APIC_LVTERR, value);
420 * spec says clear errors after enabling vector.
422 if (maxlvt > 3)
423 apic_write(APIC_ESR, 0);
424 value = apic_read(APIC_ESR);
425 if (value != oldvalue)
426 apic_printk(APIC_VERBOSE,
427 "ESR value after enabling vector: %08x, after %08x\n",
428 oldvalue, value);
429 } else {
430 if (esr_disable)
432 * Something untraceble is creating bad interrupts on
433 * secondary quads ... for the moment, just leave the
434 * ESR disabled - we can't do anything useful with the
435 * errors anyway - mbligh
437 apic_printk(APIC_DEBUG, "Leaving ESR disabled.\n");
438 else
439 apic_printk(APIC_DEBUG, "No ESR for 82489DX.\n");
442 nmi_watchdog_default();
443 if (nmi_watchdog == NMI_LOCAL_APIC)
444 setup_apic_nmi_watchdog();
445 apic_pm_activate();
448 #ifdef CONFIG_PM
450 static struct {
451 /* 'active' is true if the local APIC was enabled by us and
452 not the BIOS; this signifies that we are also responsible
453 for disabling it before entering apm/acpi suspend */
454 int active;
455 /* r/w apic fields */
456 unsigned int apic_id;
457 unsigned int apic_taskpri;
458 unsigned int apic_ldr;
459 unsigned int apic_dfr;
460 unsigned int apic_spiv;
461 unsigned int apic_lvtt;
462 unsigned int apic_lvtpc;
463 unsigned int apic_lvt0;
464 unsigned int apic_lvt1;
465 unsigned int apic_lvterr;
466 unsigned int apic_tmict;
467 unsigned int apic_tdcr;
468 unsigned int apic_thmr;
469 } apic_pm_state;
471 static int lapic_suspend(struct sys_device *dev, u32 state)
473 unsigned long flags;
475 if (!apic_pm_state.active)
476 return 0;
478 apic_pm_state.apic_id = apic_read(APIC_ID);
479 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
480 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
481 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
482 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
483 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
484 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
485 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
486 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
487 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
488 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
489 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
490 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
491 local_save_flags(flags);
492 local_irq_disable();
493 disable_local_APIC();
494 local_irq_restore(flags);
495 return 0;
498 static int lapic_resume(struct sys_device *dev)
500 unsigned int l, h;
501 unsigned long flags;
503 if (!apic_pm_state.active)
504 return 0;
506 /* XXX: Pavel needs this for S3 resume, but can't explain why */
507 set_fixmap_nocache(FIX_APIC_BASE, APIC_DEFAULT_PHYS_BASE);
509 local_irq_save(flags);
510 rdmsr(MSR_IA32_APICBASE, l, h);
511 l &= ~MSR_IA32_APICBASE_BASE;
512 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
513 wrmsr(MSR_IA32_APICBASE, l, h);
514 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
515 apic_write(APIC_ID, apic_pm_state.apic_id);
516 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
517 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
518 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
519 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
520 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
521 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
522 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
523 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
524 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
525 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
526 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
527 apic_write(APIC_ESR, 0);
528 apic_read(APIC_ESR);
529 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
530 apic_write(APIC_ESR, 0);
531 apic_read(APIC_ESR);
532 local_irq_restore(flags);
533 return 0;
536 static struct sysdev_class lapic_sysclass = {
537 set_kset_name("lapic"),
538 .resume = lapic_resume,
539 .suspend = lapic_suspend,
542 static struct sys_device device_lapic = {
543 .id = 0,
544 .cls = &lapic_sysclass,
547 static void __init apic_pm_activate(void)
549 apic_pm_state.active = 1;
552 static int __init init_lapic_sysfs(void)
554 int error;
555 if (!cpu_has_apic)
556 return 0;
557 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
558 error = sysdev_class_register(&lapic_sysclass);
559 if (!error)
560 error = sysdev_register(&device_lapic);
561 return error;
563 device_initcall(init_lapic_sysfs);
565 #else /* CONFIG_PM */
567 static void apic_pm_activate(void) { }
569 #endif /* CONFIG_PM */
571 static int __init apic_set_verbosity(char *str)
573 if (strcmp("debug", str) == 0)
574 apic_verbosity = APIC_DEBUG;
575 else if (strcmp("verbose", str) == 0)
576 apic_verbosity = APIC_VERBOSE;
577 else
578 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
579 " use apic=verbose or apic=debug", str);
581 return 0;
584 __setup("apic=", apic_set_verbosity);
587 * Detect and enable local APICs on non-SMP boards.
588 * Original code written by Keir Fraser.
589 * On AMD64 we trust the BIOS - if it says no APIC it is likely
590 * not correctly set up (usually the APIC timer won't work etc.)
593 static int __init detect_init_APIC (void)
595 if (!cpu_has_apic) {
596 printk(KERN_INFO "No local APIC present\n");
597 return -1;
600 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
601 boot_cpu_id = 0;
602 return 0;
605 void __init init_apic_mappings(void)
607 unsigned long apic_phys;
610 * If no local APIC can be found then set up a fake all
611 * zeroes page to simulate the local APIC and another
612 * one for the IO-APIC.
614 if (!smp_found_config && detect_init_APIC()) {
615 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
616 apic_phys = __pa(apic_phys);
617 } else
618 apic_phys = mp_lapic_addr;
620 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
621 apic_printk(APIC_VERBOSE,"mapped APIC to %16lx (%16lx)\n", APIC_BASE, apic_phys);
624 * Fetch the APIC ID of the BSP in case we have a
625 * default configuration (or the MP table is broken).
627 if (boot_cpu_id == -1U)
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 void __setup_APIC_LVTT(unsigned int clocks)
666 unsigned int lvtt_value, tmp_value, ver;
668 ver = GET_APIC_VERSION(apic_read(APIC_LVR));
669 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
670 if (!APIC_INTEGRATED(ver))
671 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
672 apic_write_around(APIC_LVTT, lvtt_value);
675 * Divide PICLK by 16
677 tmp_value = apic_read(APIC_TDCR);
678 apic_write_around(APIC_TDCR, (tmp_value
679 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
680 | APIC_TDR_DIV_16);
682 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
685 static void setup_APIC_timer(unsigned int clocks)
687 unsigned long flags;
689 local_irq_save(flags);
691 /* For some reasons this doesn't work on Simics, so fake it for now */
692 if (!strstr(boot_cpu_data.x86_model_id, "Screwdriver")) {
693 __setup_APIC_LVTT(clocks);
694 return;
697 /* wait for irq slice */
698 if (vxtime.hpet_address) {
699 int trigger = hpet_readl(HPET_T0_CMP);
700 while (hpet_readl(HPET_COUNTER) >= trigger)
701 /* do nothing */ ;
702 while (hpet_readl(HPET_COUNTER) < trigger)
703 /* do nothing */ ;
704 } else {
705 int c1, c2;
706 outb_p(0x00, 0x43);
707 c2 = inb_p(0x40);
708 c2 |= inb_p(0x40) << 8;
709 do {
710 c1 = c2;
711 outb_p(0x00, 0x43);
712 c2 = inb_p(0x40);
713 c2 |= inb_p(0x40) << 8;
714 } while (c2 - c1 < 300);
717 __setup_APIC_LVTT(clocks);
719 local_irq_restore(flags);
723 * In this function we calibrate APIC bus clocks to the external
724 * timer. Unfortunately we cannot use jiffies and the timer irq
725 * to calibrate, since some later bootup code depends on getting
726 * the first irq? Ugh.
728 * We want to do the calibration only once since we
729 * want to have local timer irqs syncron. CPUs connected
730 * by the same APIC bus have the very same bus frequency.
731 * And we want to have irqs off anyways, no accidental
732 * APIC irq that way.
735 #define TICK_COUNT 100000000
737 int __init calibrate_APIC_clock(void)
739 int apic, apic_start, tsc, tsc_start;
740 int result;
742 * Put whatever arbitrary (but long enough) timeout
743 * value into the APIC clock, we just want to get the
744 * counter running for calibration.
746 __setup_APIC_LVTT(1000000000);
748 apic_start = apic_read(APIC_TMCCT);
749 rdtscl(tsc_start);
751 do {
752 apic = apic_read(APIC_TMCCT);
753 rdtscl(tsc);
754 } while ((tsc - tsc_start) < TICK_COUNT && (apic - apic_start) < TICK_COUNT);
756 result = (apic_start - apic) * 1000L * cpu_khz / (tsc - tsc_start);
758 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
759 result / 1000 / 1000, result / 1000 % 1000);
761 return result * APIC_DIVISOR / HZ;
764 static unsigned int calibration_result;
766 void __init setup_boot_APIC_clock (void)
768 if (disable_apic_timer) {
769 printk(KERN_INFO "Disabling APIC timer\n");
770 return;
773 printk(KERN_INFO "Using local APIC timer interrupts.\n");
774 using_apic_timer = 1;
776 local_irq_disable();
778 calibration_result = calibrate_APIC_clock();
780 * Now set up the timer for real.
782 setup_APIC_timer(calibration_result);
784 local_irq_enable();
787 void __init setup_secondary_APIC_clock(void)
789 local_irq_disable(); /* FIXME: Do we need this? --RR */
790 setup_APIC_timer(calibration_result);
791 local_irq_enable();
794 void __init disable_APIC_timer(void)
796 if (using_apic_timer) {
797 unsigned long v;
799 v = apic_read(APIC_LVTT);
800 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
804 void enable_APIC_timer(void)
806 if (using_apic_timer) {
807 unsigned long v;
809 v = apic_read(APIC_LVTT);
810 apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
815 * the frequency of the profiling timer can be changed
816 * by writing a multiplier value into /proc/profile.
818 int setup_profiling_timer(unsigned int multiplier)
820 int i;
823 * Sanity check. [at least 500 APIC cycles should be
824 * between APIC interrupts as a rule of thumb, to avoid
825 * irqs flooding us]
827 if ( (!multiplier) || (calibration_result/multiplier < 500))
828 return -EINVAL;
831 * Set the new multiplier for each CPU. CPUs don't start using the
832 * new values until the next timer interrupt in which they do process
833 * accounting. At that time they also adjust their APIC timers
834 * accordingly.
836 for (i = 0; i < NR_CPUS; ++i)
837 per_cpu(prof_multiplier, i) = multiplier;
839 return 0;
842 #undef APIC_DIVISOR
845 * Local timer interrupt handler. It does both profiling and
846 * process statistics/rescheduling.
848 * We do profiling in every local tick, statistics/rescheduling
849 * happen only every 'profiling multiplier' ticks. The default
850 * multiplier is 1 and it can be changed by writing the new multiplier
851 * value into /proc/profile.
854 void smp_local_timer_interrupt(struct pt_regs *regs)
856 int cpu = smp_processor_id();
858 profile_tick(CPU_PROFILING, regs);
859 if (--per_cpu(prof_counter, cpu) <= 0) {
861 * The multiplier may have changed since the last time we got
862 * to this point as a result of the user writing to
863 * /proc/profile. In this case we need to adjust the APIC
864 * timer accordingly.
866 * Interrupts are already masked off at this point.
868 per_cpu(prof_counter, cpu) = per_cpu(prof_multiplier, cpu);
869 if (per_cpu(prof_counter, cpu) !=
870 per_cpu(prof_old_multiplier, cpu)) {
871 __setup_APIC_LVTT(calibration_result/
872 per_cpu(prof_counter, cpu));
873 per_cpu(prof_old_multiplier, cpu) =
874 per_cpu(prof_counter, cpu);
877 #ifdef CONFIG_SMP
878 update_process_times(user_mode(regs));
879 #endif
883 * We take the 'long' return path, and there every subsystem
884 * grabs the appropriate locks (kernel lock/ irq lock).
886 * we might want to decouple profiling from the 'long path',
887 * and do the profiling totally in assembly.
889 * Currently this isn't too much of an issue (performance wise),
890 * we can take more than 100K local irqs per second on a 100 MHz P5.
895 * Local APIC timer interrupt. This is the most natural way for doing
896 * local interrupts, but local timer interrupts can be emulated by
897 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
899 * [ if a single-CPU system runs an SMP kernel then we call the local
900 * interrupt as well. Thus we cannot inline the local irq ... ]
902 void smp_apic_timer_interrupt(struct pt_regs *regs)
905 * the NMI deadlock-detector uses this.
907 add_pda(apic_timer_irqs, 1);
910 * NOTE! We'd better ACK the irq immediately,
911 * because timer handling can be slow.
913 ack_APIC_irq();
915 * update_process_times() expects us to have done irq_enter().
916 * Besides, if we don't timer interrupts ignore the global
917 * interrupt lock, which is the WrongThing (tm) to do.
919 irq_enter();
920 smp_local_timer_interrupt(regs);
921 irq_exit();
925 * This interrupt should _never_ happen with our APIC/SMP architecture
927 asmlinkage void smp_spurious_interrupt(void)
929 unsigned int v;
930 irq_enter();
932 * Check if this really is a spurious interrupt and ACK it
933 * if it is a vectored one. Just in case...
934 * Spurious interrupts should not be ACKed.
936 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
937 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
938 ack_APIC_irq();
940 #if 0
941 static unsigned long last_warning;
942 static unsigned long skipped;
944 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
945 if (time_before(last_warning+30*HZ,jiffies)) {
946 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
947 smp_processor_id(), skipped);
948 last_warning = jiffies;
949 skipped = 0;
950 } else {
951 skipped++;
953 #endif
954 irq_exit();
958 * This interrupt should never happen with our APIC/SMP architecture
961 asmlinkage void smp_error_interrupt(void)
963 unsigned int v, v1;
965 irq_enter();
966 /* First tickle the hardware, only then report what went on. -- REW */
967 v = apic_read(APIC_ESR);
968 apic_write(APIC_ESR, 0);
969 v1 = apic_read(APIC_ESR);
970 ack_APIC_irq();
971 atomic_inc(&irq_err_count);
973 /* Here is what the APIC error bits mean:
974 0: Send CS error
975 1: Receive CS error
976 2: Send accept error
977 3: Receive accept error
978 4: Reserved
979 5: Send illegal vector
980 6: Received illegal vector
981 7: Illegal register address
983 printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
984 smp_processor_id(), v , v1);
985 irq_exit();
988 int disable_apic;
991 * This initializes the IO-APIC and APIC hardware if this is
992 * a UP kernel.
994 int __init APIC_init_uniprocessor (void)
996 if (disable_apic) {
997 printk(KERN_INFO "Apic disabled\n");
998 return -1;
1000 if (!cpu_has_apic) {
1001 disable_apic = 1;
1002 printk(KERN_INFO "Apic disabled by BIOS\n");
1003 return -1;
1006 verify_local_APIC();
1008 connect_bsp_APIC();
1010 phys_cpu_present_map = physid_mask_of_physid(0);
1011 apic_write_around(APIC_ID, boot_cpu_id);
1013 setup_local_APIC();
1015 #ifdef CONFIG_X86_IO_APIC
1016 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1017 setup_IO_APIC();
1018 else
1019 nr_ioapics = 0;
1020 #endif
1021 setup_boot_APIC_clock();
1023 return 0;
1026 static __init int setup_disableapic(char *str)
1028 disable_apic = 1;
1029 return 0;
1032 static __init int setup_nolapic(char *str)
1034 disable_apic = 1;
1035 return 0;
1038 static __init int setup_noapictimer(char *str)
1040 disable_apic_timer = 1;
1041 return 0;
1044 /* dummy parsing: see setup.c */
1046 __setup("disableapic", setup_disableapic);
1047 __setup("nolapic", setup_nolapic); /* same as disableapic, for compatibility */
1049 __setup("noapictimer", setup_noapictimer);
1051 /* no "lapic" flag - we only use the lapic when the BIOS tells us so. */