diff -Naur linux-2.6.14.3/arch/i386/kernel/apm.c linux-2.6.14.3-pmtmr-v4/arch/i386/kernel/apm.c --- linux-2.6.14.3/arch/i386/kernel/apm.c +++ linux-2.6.14.3-pmtmr-v4/arch/i386/kernel/apm.c @@ -229,6 +229,7 @@ #include #include #include +#include #include "io_ports.h" @@ -1167,9 +1168,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.14.3/arch/i386/kernel/time.c linux-2.6.14.3-pmtmr-v4/arch/i386/kernel/time.c --- linux-2.6.14.3/arch/i386/kernel/time.c +++ linux-2.6.14.3-pmtmr-v4/arch/i386/kernel/time.c @@ -86,6 +86,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.14.3/arch/i386/kernel/timers/common.c linux-2.6.14.3-pmtmr-v4/arch/i386/kernel/timers/common.c --- linux-2.6.14.3/arch/i386/kernel/timers/common.c +++ linux-2.6.14.3-pmtmr-v4/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.14.3/arch/i386/kernel/timers/timer_pit.c linux-2.6.14.3-pmtmr-v4/arch/i386/kernel/timers/timer_pit.c --- linux-2.6.14.3/arch/i386/kernel/timers/timer_pit.c +++ linux-2.6.14.3-pmtmr-v4/arch/i386/kernel/timers/timer_pit.c @@ -115,8 +115,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; } @@ -169,8 +169,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.14.3/arch/i386/kernel/timers/timer_pm.c linux-2.6.14.3-pmtmr-v4/arch/i386/kernel/timers/timer_pm.c --- linux-2.6.14.3/arch/i386/kernel/timers/timer_pm.c +++ linux-2.6.14.3-pmtmr-v4/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.14.3/arch/i386/kernel/timers/timer_tsc.c linux-2.6.14.3-pmtmr-v4/arch/i386/kernel/timers/timer_tsc.c --- linux-2.6.14.3/arch/i386/kernel/timers/timer_tsc.c +++ linux-2.6.14.3-pmtmr-v4/arch/i386/kernel/timers/timer_tsc.c @@ -379,8 +379,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.14.3/arch/x86_64/kernel/i8259.c linux-2.6.14.3-pmtmr-v4/arch/x86_64/kernel/i8259.c --- linux-2.6.14.3/arch/x86_64/kernel/i8259.c +++ linux-2.6.14.3-pmtmr-v4/arch/x86_64/kernel/i8259.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -498,9 +499,9 @@ { outb_p(0x34,0x43); /* binary, mode 2, LSB/MSB, ch 0 */ udelay(10); - outb_p(LATCH & 0xff , 0x40); /* LSB */ + outb_p((LATCH * timer_mult / 1000) & 0xff , 0x40); /* LSB */ udelay(10); - outb(LATCH >> 8 , 0x40); /* MSB */ + outb((LATCH * timer_mult / 1000) >> 8 , 0x40); /* MSB */ } static int timer_resume(struct sys_device *dev) diff -Naur linux-2.6.14.3/arch/x86_64/kernel/pmtimer.c linux-2.6.14.3-pmtmr-v4/arch/x86_64/kernel/pmtimer.c --- linux-2.6.14.3/arch/x86_64/kernel/pmtimer.c +++ linux-2.6.14.3-pmtmr-v4/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.14.3/arch/x86_64/kernel/time.c linux-2.6.14.3-pmtmr-v4/arch/x86_64/kernel/time.c --- linux-2.6.14.3/arch/x86_64/kernel/time.c +++ linux-2.6.14.3-pmtmr-v4/arch/x86_64/kernel/time.c @@ -46,6 +46,12 @@ EXPORT_SYMBOL(jiffies_64); +/* + * 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; + #ifdef CONFIG_CPU_FREQ static void cpufreq_delayed_get(void); #endif @@ -57,6 +63,7 @@ static int nohpet __initdata = 0; static int notsc __initdata = 0; +static int use_pmtimer __initdata = 0; #undef HPET_HACK_ENABLE_DANGEROUS @@ -728,6 +735,11 @@ outb((inb(0x61) & ~0x02) | 0x01, 0x61); outb(0xb0, 0x43); + + /* + * (PIT_TICK_RATE / (1000 / 50)) is 59659, + * so we can't use the timer_mult here. + */ outb((PIT_TICK_RATE / (1000 / 50)) & 0xff, 0x42); outb((PIT_TICK_RATE / (1000 / 50)) >> 8, 0x42); rdtscll(start); @@ -738,7 +750,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 @@ -867,8 +883,8 @@ spin_lock_irqsave(&i8253_lock, flags); outb_p(0x34, 0x43); /* binary, mode 2, LSB/MSB, ch 0 */ - outb_p(LATCH & 0xff, 0x40); /* LSB */ - outb_p(LATCH >> 8, 0x40); /* MSB */ + outb_p((LATCH * timer_mult / 1000) & 0xff, 0x40); /* LSB */ + outb_p((LATCH * timer_mult / 1000) >> 8, 0x40); /* MSB */ spin_unlock_irqrestore(&i8253_lock, flags); } @@ -913,11 +929,11 @@ vxtime_hz = (1000000000000000L + hpet_period / 2) / hpet_period; - if (hpet_use_timer) { + if (hpet_use_timer && !use_pmtimer) { cpu_khz = hpet_calibrate_tsc(); timename = "HPET"; #ifdef CONFIG_X86_PM_TIMER - } else if (pmtmr_ioport) { + } else if ((pmtmr_ioport) && verify_pmtmr_rate(use_pmtimer)) { vxtime_hz = PM_TIMER_FREQUENCY; timename = "PM"; pit_init(); @@ -1304,4 +1320,13 @@ __setup("notsc", notsc_setup); +static int __init pmtmr_setup(char *s) +{ + use_pmtimer = 1; + return 0; +} + +__setup("pmtmr", pmtmr_setup); + + diff -Naur linux-2.6.14.3/Documentation/kernel-parameters.txt linux-2.6.14.3-pmtmr-v4/Documentation/kernel-parameters.txt --- linux-2.6.14.3/Documentation/kernel-parameters.txt +++ linux-2.6.14.3-pmtmr-v4/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 } hpet= [IA-32,HPET] option to disable HPET and use PIT. diff -Naur linux-2.6.14.3/Documentation/x86_64/boot-options.txt linux-2.6.14.3-pmtmr-v4/Documentation/x86_64/boot-options.txt --- linux-2.6.14.3/Documentation/x86_64/boot-options.txt +++ linux-2.6.14.3-pmtmr-v4/Documentation/x86_64/boot-options.txt @@ -59,6 +59,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.14.3/include/asm-i386/timer.h linux-2.6.14.3-pmtmr-v4/include/asm-i386/timer.h --- linux-2.6.14.3/include/asm-i386/timer.h +++ linux-2.6.14.3-pmtmr-v4/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.14.3/include/asm-x86_64/proto.h linux-2.6.14.3-pmtmr-v4/include/asm-x86_64/proto.h --- linux-2.6.14.3/include/asm-x86_64/proto.h +++ linux-2.6.14.3-pmtmr-v4/include/asm-x86_64/proto.h @@ -38,6 +38,8 @@ extern void time_init_gtod(void); extern int pmtimer_mark_offset(void); extern unsigned int do_gettimeoffset_pm(void); +extern int verify_pmtmr_rate(int); +extern u16 timer_mult; extern u32 pmtmr_ioport; extern unsigned long long monotonic_base; extern int sysctl_vsyscall;