diff -Naur linux-2.6.18/arch/x86_64/kernel/pmtimer.c linux-2.6.18-pmtmr/arch/x86_64/kernel/pmtimer.c --- linux-2.6.18/arch/x86_64/kernel/pmtimer.c +++ linux-2.6.18-pmtmr/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.18/arch/x86_64/kernel/time.c linux-2.6.18-pmtmr/arch/x86_64/kernel/time.c --- linux-2.6.18/arch/x86_64/kernel/time.c +++ linux-2.6.18-pmtmr/arch/x86_64/kernel/time.c @@ -57,6 +57,7 @@ int nohpet __initdata = 0; static int notsc __initdata = 0; +static int pmtmr __initdata = 0; #define USEC_PER_TICK (USEC_PER_SEC / HZ) #define NSEC_PER_TICK (NSEC_PER_SEC / HZ) @@ -73,6 +74,7 @@ unsigned long vxtime_hz = PIT_TICK_RATE; int report_lost_ticks; /* command line option */ unsigned long long monotonic_base; +u16 timer_mult = 1000; struct vxtime_data __vxtime __section_vxtime; /* for vsyscalls */ @@ -716,6 +718,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); @@ -725,7 +731,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 @@ -855,8 +865,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); } @@ -918,7 +928,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(); @@ -1323,3 +1334,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.18/include/asm-x86_64/proto.h linux-2.6.18-pmtmr/include/asm-x86_64/proto.h --- linux-2.6.18/include/asm-x86_64/proto.h +++ linux-2.6.18-pmtmr/include/asm-x86_64/proto.h @@ -42,6 +42,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