diff -Naur linux-2.6.16.orig/arch/i386/kernel/apm.c linux-2.6.16/arch/i386/kernel/apm.c --- linux-2.6.16.orig/arch/i386/kernel/apm.c +++ linux-2.6.16/arch/i386/kernel/apm.c @@ -231,6 +231,7 @@ #include #include #include +#include #include "io_ports.h" @@ -1179,9 +1180,9 @@ /* set the clock to 100 Hz */ outb_p(0x34, PIT_MODE); /* binary, mode 2, LSB/MSB, ch 0 */ udelay(10); - outb_p(LATCH & 0xff, PIT_CH0); /* LSB */ + outb_p((LATCH * timer_mult / 1000) & 0xff, PIT_CH0); /* LSB */ udelay(10); - outb(LATCH >> 8, PIT_CH0); /* MSB */ + outb((LATCH * timer_mult / 1000) >> 8, PIT_CH0); /* MSB */ udelay(10); spin_unlock_irqrestore(&i8253_lock, flags); #endif diff -Naur linux-2.6.16.orig/arch/i386/kernel/time.c linux-2.6.16/arch/i386/kernel/time.c --- linux-2.6.16.orig/arch/i386/kernel/time.c +++ linux-2.6.16/arch/i386/kernel/time.c @@ -82,6 +82,12 @@ DEFINE_SPINLOCK(rtc_lock); EXPORT_SYMBOL(rtc_lock); +/* + * timer_mult is a mutiplier used to work around some very buggy + * hardware where the PIT timer runs way too fast. + */ +u16 timer_mult = 1000; + #include DEFINE_SPINLOCK(i8253_lock); diff -Naur linux-2.6.16.orig/arch/i386/kernel/timers/common.c linux-2.6.16/arch/i386/kernel/timers/common.c --- linux-2.6.16.orig/arch/i386/kernel/timers/common.c +++ linux-2.6.16/arch/i386/kernel/timers/common.c @@ -23,7 +23,7 @@ * device. */ -#define CALIBRATE_TIME (5 * 1000020/HZ) +#define CALIBRATE_TIME (5 * 1000020/(HZ * timer_mult / 1000)) unsigned long calibrate_tsc(void) { diff -Naur linux-2.6.16.orig/arch/i386/kernel/timers/timer_pit.c linux-2.6.16/arch/i386/kernel/timers/timer_pit.c --- linux-2.6.16.orig/arch/i386/kernel/timers/timer_pit.c +++ linux-2.6.16/arch/i386/kernel/timers/timer_pit.c @@ -116,8 +116,8 @@ /* VIA686a test code... reset the latch if count > max + 1 */ if (count > LATCH) { outb_p(0x34, PIT_MODE); - outb_p(LATCH & 0xff, PIT_CH0); - outb(LATCH >> 8, PIT_CH0); + outb_p((LATCH * timer_mult / 1000) & 0xff, PIT_CH0); + outb((LATCH * timer_mult / 1000) >> 8, PIT_CH0); count = LATCH - 1; } @@ -170,8 +170,8 @@ spin_lock_irqsave(&i8253_lock, flags); outb_p(0x34,PIT_MODE); /* binary, mode 2, LSB/MSB, ch 0 */ udelay(10); - outb_p(LATCH & 0xff , PIT_CH0); /* LSB */ + outb_p((LATCH * timer_mult / 1000) & 0xff , PIT_CH0); /* LSB */ udelay(10); - outb(LATCH >> 8 , PIT_CH0); /* MSB */ + outb((LATCH * timer_mult / 1000) >> 8 , PIT_CH0); /* MSB */ spin_unlock_irqrestore(&i8253_lock, flags); } diff -Naur linux-2.6.16.orig/arch/i386/kernel/timers/timer_pm.c linux-2.6.16/arch/i386/kernel/timers/timer_pm.c --- linux-2.6.16.orig/arch/i386/kernel/timers/timer_pm.c +++ linux-2.6.16/arch/i386/kernel/timers/timer_pm.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -23,6 +24,7 @@ #include #include "mach_timer.h" +#include "io_ports.h" /* Number of PMTMR ticks expected during calibration run */ #define PMTMR_TICKS_PER_SEC 3579545 @@ -35,6 +37,8 @@ * in arch/i386/acpi/boot.c */ u32 pmtmr_ioport = 0; +extern spinlock_t i8259A_lock; +extern spinlock_t i8253_lock; /* value of the Power timer at last timer interrupt */ static u32 offset_tick; @@ -66,11 +70,24 @@ } +static void reinit_timer(void) +{ + unsigned long flags; + + spin_lock_irqsave(&i8253_lock, flags); + outb_p(0x34, PIT_MODE); /* binary, mode 2, LSB/MSB, ch 0 */ + udelay(10); + outb_p((LATCH * timer_mult / 1000) & 0xff, PIT_CH0); /* LSB */ + udelay(10); + outb((LATCH * timer_mult / 1000) >> 8, PIT_CH0); /* MSB */ + spin_unlock_irqrestore(&i8253_lock, flags); +} + /* * Some boards have the PMTMR running way too fast. We check * the PMTMR rate against PIT channel 2 to catch these cases. */ -static int verify_pmtmr_rate(void) +static int verify_pmtmr_rate(char* override) { u32 value1, value2; unsigned long count, delta; @@ -81,9 +98,19 @@ value2 = read_pmtmr(); delta = (value2 - value1) & ACPI_PM_MASK; + /* Check that the PMTMR delta is within 5% of what we expect */ if (delta < (PMTMR_EXPECTED_RATE * 19) / 20 || delta > (PMTMR_EXPECTED_RATE * 21) / 20) { + + timer_mult = PMTMR_EXPECTED_RATE * 1000 / delta; + if (override[0]) { + printk(KERN_INFO "PIT running at invalid rate, workaround multiplier set to %d.%d\n", timer_mult / 1000, timer_mult % 1000); + reinit_timer(); + + return 0; + } + printk(KERN_INFO "PM-Timer running at invalid rate: %lu%% of normal - aborting.\n", 100UL * delta / PMTMR_EXPECTED_RATE); return -1; } @@ -124,7 +151,7 @@ return -ENODEV; pm_good: - if (verify_pmtmr_rate() != 0) + if (verify_pmtmr_rate(override) != 0) return -ENODEV; init_cpu_khz(); diff -Naur linux-2.6.16.orig/arch/i386/kernel/timers/timer_tsc.c linux-2.6.16/arch/i386/kernel/timers/timer_tsc.c --- linux-2.6.16.orig/arch/i386/kernel/timers/timer_tsc.c +++ linux-2.6.16/arch/i386/kernel/timers/timer_tsc.c @@ -401,8 +401,8 @@ */ if (count > LATCH) { outb_p(0x34, PIT_MODE); - outb_p(LATCH & 0xff, PIT_CH0); - outb(LATCH >> 8, PIT_CH0); + outb_p((LATCH * timer_mult / 1000) & 0xff, PIT_CH0); + outb((LATCH * timer_mult / 1000) >> 8, PIT_CH0); count = LATCH - 1; } diff -Naur linux-2.6.16.orig/arch/x86_64/kernel/pmtimer.c linux-2.6.16/arch/x86_64/kernel/pmtimer.c --- linux-2.6.16.orig/arch/x86_64/kernel/pmtimer.c +++ linux-2.6.16/arch/x86_64/kernel/pmtimer.c @@ -35,6 +35,49 @@ #define ACPI_PM_MASK 0xFFFFFF /* limit it to 24 bits */ + +#define PMTMR_TICKS_PER_SEC 3579545 +#define PMTMR_EXPECTED_RATE \ + ((LATCH * (PMTMR_TICKS_PER_SEC >> 10)) / (CLOCK_TICK_RATE>>10)) + +int verify_pmtmr_rate(int override) +{ + u32 value1, value2; + unsigned long delta; + unsigned long count = 0; + + outb((inb(0x61) & ~0x02) | 0x01, 0x61); + outb(0xb0, 0x43); + outb_p(LATCH & 0xff, 0x42); + outb_p(LATCH >> 8, 0x42); + + value1 = inl(pmtmr_ioport) & ACPI_PM_MASK; + + do { + count++; + } while ((inb_p(0x61) & 0x20) == 0); + + value2 = inl(pmtmr_ioport) & ACPI_PM_MASK; + delta = (value2 - value1) & ACPI_PM_MASK; + + /* Check that the PMTMR delta is within 5% of what we expect */ + if (delta < (PMTMR_EXPECTED_RATE * 19) / 20 || + delta > (PMTMR_EXPECTED_RATE * 21) / 20) { + + timer_mult = PMTMR_EXPECTED_RATE * 1000 / delta; + if (override) { + printk(KERN_INFO "PIT running at invalid rate, workaround multiplier set to %d.%d\n", timer_mult / 1000, timer_mult % 1000); + return 0; + } + + printk(KERN_INFO "PM-Timer running at invalid rate: %lu%% of normal - aborting.\n", 100UL * delta / PMTMR_EXPECTED_RATE); + return -1; + } + + timer_mult = 1000; + return 0; +} + static inline u32 cyc2us(u32 cycles) { /* The Power Management Timer ticks at 3.579545 ticks per microsecond. diff -Naur linux-2.6.16.orig/arch/x86_64/kernel/time.c linux-2.6.16/arch/x86_64/kernel/time.c --- linux-2.6.16.orig/arch/x86_64/kernel/time.c +++ linux-2.6.16/arch/x86_64/kernel/time.c @@ -55,6 +55,7 @@ int nohpet __initdata = 0; static int notsc __initdata = 0; +static int pmtmr __initdata = 0; #undef HPET_HACK_ENABLE_DANGEROUS @@ -65,6 +66,7 @@ unsigned long vxtime_hz = PIT_TICK_RATE; int report_lost_ticks; /* command line option */ unsigned long long monotonic_base; +u16 timer_mult = 1000; /* for heavy broken PIT timers */ struct vxtime_data __vxtime __section_vxtime; /* for vsyscalls */ @@ -721,6 +723,10 @@ outb((inb(0x61) & ~0x02) | 0x01, 0x61); + /* + * (PIT_TICK_RATE / (1000 / 50)) is 59659, + * so we can't use the timer_mult here. + */ outb(0xb0, 0x43); outb((PIT_TICK_RATE / (1000 / 50)) & 0xff, 0x42); outb((PIT_TICK_RATE / (1000 / 50)) >> 8, 0x42); @@ -730,7 +736,11 @@ spin_unlock_irqrestore(&i8253_lock, flags); - return (end - start) / 50; + /* + * It's safer to use timer_mult on the + * computed value. + */ + return timer_mult * (end - start) / (50 * 1000); } #ifdef CONFIG_HPET @@ -862,8 +872,8 @@ spin_lock_irqsave(&i8253_lock, flags); outb_p(mode, PIT_MODE); - outb_p(val & 0xff, PIT_CH0); /* LSB */ - outb_p(val >> 8, PIT_CH0); /* MSB */ + outb_p((val * timer_mult / 1000) & 0xff, PIT_CH0); /* LSB */ + outb_p((val * timer_mult / 1000) >> 8, PIT_CH0); /* MSB */ spin_unlock_irqrestore(&i8253_lock, flags); } @@ -936,7 +946,8 @@ cpu_khz = hpet_calibrate_tsc(); timename = "HPET"; #ifdef CONFIG_X86_PM_TIMER - } else if (pmtmr_ioport && !vxtime.hpet_address) { + } else if (pmtmr_ioport && !vxtime.hpet_address && + verify_pmtmr_rate(pmtmr)) { vxtime_hz = PM_TIMER_FREQUENCY; timename = "PM"; pit_init(); @@ -1334,3 +1345,11 @@ } __setup("notsc", notsc_setup); + +static int __init pmtmr_setup(char *s) +{ + pmtmr = 1; + return 0; +} + +__setup("pmtmr", pmtmr_setup); diff -Naur linux-2.6.16.orig/Documentation/kernel-parameters.txt linux-2.6.16/Documentation/kernel-parameters.txt --- linux-2.6.16.orig/Documentation/kernel-parameters.txt +++ linux-2.6.16/Documentation/kernel-parameters.txt @@ -333,6 +333,10 @@ Forces specified timesource (if avaliable) to be used when calculating gettimeofday(). If specicified timesource is not avalible, it defaults to PIT. + When timesource is set to pmtmr manually, the PM timer is + assumed to be a reliable time source and the PIT/TSC is + adjusted accordingly. This can work around some very buggy + hardware where the PIT timer runs way too fast. Format: { pit | tsc | cyclone | pmtmr } disable_8254_timer diff -Naur linux-2.6.16.orig/Documentation/x86_64/boot-options.txt linux-2.6.16/Documentation/x86_64/boot-options.txt --- linux-2.6.16.orig/Documentation/x86_64/boot-options.txt +++ linux-2.6.16/Documentation/x86_64/boot-options.txt @@ -77,6 +77,12 @@ This can be used to work around timing problems on multiprocessor systems with not properly synchronized CPUs. + + pmtmr + Assume the PM timer is a reliable time source and adjust the PIT/TSC accordingly. + This can work around some very buggy hardware where the PIT timer runs way too + fast. + report_lost_ticks Report when timer interrupts are lost because some code turned off interrupts for too long. diff -Naur linux-2.6.16.orig/include/asm-i386/timer.h linux-2.6.16/include/asm-i386/timer.h --- linux-2.6.16.orig/include/asm-i386/timer.h +++ linux-2.6.16/include/asm-i386/timer.h @@ -43,6 +43,12 @@ extern int pit_latch_buggy; +/* + * timer_mult is a mutiplier used to work around some very buggy + * hardware where the PIT timer runs way too fast. + */ +extern u16 timer_mult; + extern struct timer_opts *cur_timer; extern int timer_ack; diff -Naur linux-2.6.16.orig/include/asm-x86_64/proto.h linux-2.6.16/include/asm-x86_64/proto.h --- linux-2.6.16.orig/include/asm-x86_64/proto.h +++ linux-2.6.16/include/asm-x86_64/proto.h @@ -43,6 +43,8 @@ extern void pmtimer_resume(void); extern void pmtimer_wait(unsigned); extern unsigned int do_gettimeoffset_pm(void); +extern int verify_pmtmr_rate(int); +extern u16 timer_mult; #ifdef CONFIG_X86_PM_TIMER extern u32 pmtmr_ioport; #else