Commit 1c41006f authored by Andi Kleen's avatar Andi Kleen Committed by Linus Torvalds

[PATCH] Time changes for x86-64

Some timer updates from Vojtech Pavlik for x86-64.  In theory support
HPET timing now, but the support is disabled.

Would actually need vxtime_lock() macros in the generic timer code to
protect xtime updates, but I'm leaving that out now because it's only
needed for vsyscalls and they're currently disabled.
parent 15aed72d
/* /*
* linux/arch/x86-64/kernel/time.c * linux/arch/x86-64/kernel/time.c
* *
* Copyright (C) 1991, 1992, 1995 Linus Torvalds * "High Precision Event Timer" based timekeeping.
*
* Copyright (c) 1991,1992,1995 Linus Torvalds
* Copyright (c) 1994 Alan Modra
* Copyright (c) 1995 Markus Kuhn
* Copyright (c) 1996 Ingo Molnar
* Copyright (c) 1998 Andrea Arcangeli
* Copyright (c) 2002 Vojtech Pavlik
* *
* This file contains the PC-specific time handling details:
* reading the RTC at bootup, etc..
* 1994-07-02 Alan Modra
* fixed set_rtc_mmss, fixed time.year for >= 2000, new mktime
* 1995-03-26 Markus Kuhn
* fixed 500 ms bug at call to set_rtc_mmss, fixed DS12887
* precision CMOS clock update
* 1996-05-03 Ingo Molnar
* fixed time warps in do_[slow|fast]_gettimeoffset()
* 1997-09-10 Updated NTP code according to technical memorandum Jan '96
* "A Kernel Model for Precision Timekeeping" by Dave Mills
* 1998-09-05 (Various)
* More robust do_fast_gettimeoffset() algorithm implemented
* (works with APM, Cyrix 6x86MX and Centaur C6),
* monotonic gettimeofday() with fast_get_timeoffset(),
* drift-proof precision TSC calibration on boot
* (C. Scott Ananian <cananian@alumni.princeton.edu>, Andrew D.
* Balsa <andrebalsa@altern.org>, Philip Gladstone <philip@raptor.com>;
* ported from 2.0.35 Jumbo-9 by Michael Krause <m.krause@tu-harburg.de>).
* 1998-12-16 Andrea Arcangeli
* Fixed Jumbo-9 code in 2.1.131: do_gettimeofday was missing 1 jiffy
* because was not accounting lost_ticks.
* 1998-12-24 Copyright (C) 1998 Andrea Arcangeli
* Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
* serialize accesses to xtime/lost_ticks).
*/ */
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/param.h> #include <linux/sched.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/time.h>
#include <linux/delay.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/smp.h>
#include <linux/device.h>
#include <asm/io.h>
#include <asm/smp.h>
#include <asm/irq.h>
#include <asm/msr.h>
#include <asm/delay.h>
#include <asm/mpspec.h>
#include <asm/uaccess.h>
#include <asm/processor.h>
#include <linux/mc146818rtc.h> #include <linux/mc146818rtc.h>
#include <linux/timex.h>
#include <linux/config.h>
#include <asm/fixmap.h>
#include <linux/irq.h> #include <linux/irq.h>
#include <linux/ioport.h>
#include <linux/module.h>
#include <linux/device.h>
#include <asm/vsyscall.h>
#include <asm/timex.h>
unsigned int cpu_khz; /* Detected as we calibrate the TSC */ u64 jiffies_64;
/* Number of usecs that the last interrupt was delayed */
int __delay_at_last_interrupt __section_delay_at_last_interrupt;
unsigned int __last_tsc_low __section_last_tsc_low; /* lsb 32 bits of Time Stamp Counter */
/* Cached *multiplier* to convert TSC counts to microseconds.
* (see the equation below).
* Equal to 2^32 * (1 / (clocks per usec) ).
* Initialized in time_init.
*/
unsigned int __fast_gettimeoffset_quotient __section_fast_gettimeoffset_quotient;
extern rwlock_t xtime_lock; extern rwlock_t xtime_lock;
struct timeval __xtime __section_xtime;
unsigned long __wall_jiffies __section_wall_jiffies;
struct timezone __sys_tz __section_sys_tz;
volatile unsigned long __jiffies __section_jiffies;
spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED; spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
inline unsigned long do_gettimeoffset(void) unsigned int cpu_khz; /* TSC clocks / usec, not used here */
{ unsigned long hpet_period; /* fsecs / HPET clock */
register unsigned int eax, edx; unsigned long hpet_tick; /* HPET clocks / interrupt */
int hpet_report_lost_ticks; /* command line option */
/* Read the Time Stamp Counter */
rdtsc(eax,edx); struct hpet_data __hpet __section_hpet; /* address, quotient, trigger, hz */
/* .. relative to previous jiffy (32 bits is enough) */ volatile unsigned long __jiffies __section_jiffies;
eax -= last_tsc_low; /* tsc_low delta */ unsigned long __wall_jiffies __section_wall_jiffies;
struct timespec __xtime __section_xtime;
struct timezone __sys_tz __section_sys_tz;
/* /*
* Time offset = (tsc_low delta) * fast_gettimeoffset_quotient * do_gettimeoffset() returns microseconds since last timer interrupt was
* = (tsc_low delta) * (usecs_per_clock) * triggered by hardware. A memory read of HPET is slower than a register read
* = (tsc_low delta) * (usecs_per_jiffy / clocks_per_jiffy) * of TSC, but much more reliable. It's also synchronized to the timer
* * interrupt. Note that do_gettimeoffset() may return more than hpet_tick, if a
* Using a mull instead of a divl saves up to 31 clock cycles * timer interrupt has happened already, but hpet.trigger wasn't updated yet.
* in the critical path. * This is not a problem, because jiffies hasn't updated either. They are bound
* together by xtime_lock.
*/ */
__asm__("mull %2" static spinlock_t time_offset_lock = SPIN_LOCK_UNLOCKED;
:"=a" (eax), "=d" (edx) static unsigned long timeoffset = 0;
:"rm" (fast_gettimeoffset_quotient),
"0" (eax));
/* our adjusted time offset in microseconds */ inline unsigned int do_gettimeoffset(void)
return delay_at_last_interrupt + edx; {
unsigned long t;
rdtscll(t);
return (t - hpet.last_tsc) * (1000000L / HZ) / hpet.ticks + hpet.offset;
} }
#define TICK_SIZE tick
spinlock_t i8253_lock = SPIN_LOCK_UNLOCKED;
extern spinlock_t i8259A_lock;
/* /*
* This version of gettimeofday has microsecond resolution * This version of gettimeofday() has microsecond resolution and better than
* and better than microsecond precision on fast x86 machines with TSC. * microsecond precision, as we're using at least a 10 MHz (usually 14.31818
* MHz) HPET timer.
*/ */
void do_gettimeofday(struct timeval *tv) void do_gettimeofday(struct timeval *tv)
{ {
unsigned long flags; unsigned long flags, t;
unsigned long usec, sec; unsigned int sec, usec;
read_lock_irqsave(&xtime_lock, flags); read_lock_irqsave(&xtime_lock, flags);
usec = do_gettimeoffset(); spin_lock(&time_offset_lock);
{
unsigned long lost = jiffies - wall_jiffies;
if (lost)
usec += lost * (1000000 / HZ);
}
sec = xtime.tv_sec; sec = xtime.tv_sec;
usec += xtime.tv_usec; usec = xtime.tv_nsec / 1000;
read_unlock_irqrestore(&xtime_lock, flags);
while (usec >= 1000000) { t = (jiffies - wall_jiffies) * (1000000L / HZ) + do_gettimeoffset();
usec -= 1000000; if (t > timeoffset) timeoffset = t;
sec++; usec += timeoffset;
}
spin_unlock(&time_offset_lock);
read_unlock_irqrestore(&xtime_lock, flags);
tv->tv_sec = sec; tv->tv_sec = sec + usec / 1000000;
tv->tv_usec = usec; tv->tv_usec = usec % 1000000;
} }
/*
* settimeofday() first undoes the correction that gettimeofday would do
* on the time, and then saves it. This is ugly, but has been like this for
* ages already.
*/
void do_settimeofday(struct timeval *tv) void do_settimeofday(struct timeval *tv)
{ {
write_lock_irq(&xtime_lock); write_lock_irq(&xtime_lock);
vxtime_lock(); vxtime_lock();
/*
* This is revolting. We need to set "xtime" correctly. However, the tv->tv_usec -= do_gettimeoffset() +
* value in this location is the value at the most recent update of (jiffies - wall_jiffies) * tick_usec;
* wall time. Discover what correction gettimeofday() would have
* made, and then undo it!
*/
tv->tv_usec -= do_gettimeoffset();
tv->tv_usec -= (jiffies - wall_jiffies) * (1000000 / HZ);
while (tv->tv_usec < 0) { while (tv->tv_usec < 0) {
tv->tv_usec += 1000000; tv->tv_usec += 1000000;
tv->tv_sec--; tv->tv_sec--;
} }
xtime = *tv; xtime.tv_sec = tv->tv_sec;
xtime.tv_nsec = (tv->tv_usec * 1000);
vxtime_unlock(); vxtime_unlock();
time_adjust = 0; /* stop active adjtime() */ time_adjust = 0; /* stop active adjtime() */
time_status |= STA_UNSYNC; time_status |= STA_UNSYNC;
time_maxerror = NTP_PHASE_LIMIT; time_maxerror = NTP_PHASE_LIMIT;
time_esterror = NTP_PHASE_LIMIT; time_esterror = NTP_PHASE_LIMIT;
write_unlock_irq(&xtime_lock); write_unlock_irq(&xtime_lock);
} }
/* /*
* In order to set the CMOS clock precisely, set_rtc_mmss has to be * In order to set the CMOS clock precisely, set_rtc_mmss has to be called 500
* called 500 ms after the second nowtime has started, because when * ms after the second nowtime has started, because when nowtime is written
* nowtime is written into the registers of the CMOS clock, it will * into the registers of the CMOS clock, it will jump to the next second
* jump to the next second precisely 500 ms later. Check the Motorola * precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data
* MC146818A or Dallas DS12887 data sheet for details. * sheet for details.
*
* BUG: This routine does not handle hour overflow properly; it just
* sets the minutes. Usually you'll only notice that after reboot!
*/ */
static int set_rtc_mmss(unsigned long nowtime)
static void set_rtc_mmss(unsigned long nowtime)
{ {
int retval = 0;
int real_seconds, real_minutes, cmos_minutes; int real_seconds, real_minutes, cmos_minutes;
unsigned char save_control, save_freq_select; unsigned char control, freq_select;
/*
* IRQs are disabled when we're called from the timer interrupt,
* no need for spin_lock_irqsave()
*/
/* gets recalled with irq locally disabled */
spin_lock(&rtc_lock); spin_lock(&rtc_lock);
save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */ /*
CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT); * Tell the clock it's being set and stop it.
*/
control = CMOS_READ(RTC_CONTROL);
CMOS_WRITE(control | RTC_SET, RTC_CONTROL);
freq_select = CMOS_READ(RTC_FREQ_SELECT);
CMOS_WRITE(freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT);
cmos_minutes = CMOS_READ(RTC_MINUTES); cmos_minutes = CMOS_READ(RTC_MINUTES);
if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
BCD_TO_BIN(cmos_minutes); BCD_TO_BIN(cmos_minutes);
/* /*
* since we're only adjusting minutes and seconds, * since we're only adjusting minutes and seconds, don't interfere with hour
* don't interfere with hour overflow. This avoids * overflow. This avoids messing with unknown time zones but requires your RTC
* messing with unknown time zones but requires your * not to be off by more than 15 minutes. Since we're calling it only when
* RTC not to be off by more than 15 minutes * our clock is externally synchronized using NTP, this shouldn't be a problem.
*/ */
real_seconds = nowtime % 60; real_seconds = nowtime % 60;
real_minutes = nowtime / 60; real_minutes = nowtime / 60;
if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1) if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
real_minutes += 30; /* correct for half hour time zone */ real_minutes += 30; /* correct for half hour time zone */
real_minutes %= 60; real_minutes %= 60;
if (abs(real_minutes - cmos_minutes) < 30) { if (abs(real_minutes - cmos_minutes) < 30) {
if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
BIN_TO_BCD(real_seconds); BIN_TO_BCD(real_seconds);
BIN_TO_BCD(real_minutes); BIN_TO_BCD(real_minutes);
} CMOS_WRITE(real_seconds, RTC_SECONDS);
CMOS_WRITE(real_seconds,RTC_SECONDS); CMOS_WRITE(real_minutes, RTC_MINUTES);
CMOS_WRITE(real_minutes,RTC_MINUTES); } else
} else { printk(KERN_WARNING "time.c: can't update CMOS clock from %d to %d\n",
printk(KERN_WARNING
"set_rtc_mmss: can't update from %d to %d\n",
cmos_minutes, real_minutes); cmos_minutes, real_minutes);
retval = -1;
}
/* The following flags have to be released exactly in this order, /*
* otherwise the DS12887 (popular MC146818A clone with integrated * The following flags have to be released exactly in this order, otherwise the
* battery and quartz) will not reset the oscillator and will not * DS12887 (popular MC146818A clone with integrated battery and quartz) will
* update precisely 500 ms later. You won't find this mentioned in * not reset the oscillator and will not update precisely 500 ms later. You
* the Dallas Semiconductor data sheets, but who believes data * won't find this mentioned in the Dallas Semiconductor data sheets, but who
* sheets anyway ... -- Markus Kuhn * believes data sheets anyway ... -- Markus Kuhn
*/ */
CMOS_WRITE(save_control, RTC_CONTROL);
CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
spin_unlock(&rtc_lock);
return retval; CMOS_WRITE(control, RTC_CONTROL);
} CMOS_WRITE(freq_select, RTC_FREQ_SELECT);
/* last time the cmos clock got updated */ spin_unlock(&rtc_lock);
static long last_rtc_update; }
int timer_ack;
/* static void timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
* timer_interrupt() needs to keep up the real-time clock,
* as well as call the "do_timer()" routine every clocktick
*/
static inline void do_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{ {
#ifdef CONFIG_X86_IO_APIC static unsigned long rtc_update = 0;
if (timer_ack) {
/*
* Subtle, when I/O APICs are used we have to ack timer IRQ
* manually to reset the IRR bit for do_slow_gettimeoffset().
* This will also deassert NMI lines for the watchdog if run
* on an 82489DX-based system.
*/
spin_lock(&i8259A_lock);
outb(0x0c, 0x20);
/* Ack the IRQ; AEOI will end it automatically. */
inb(0x20);
spin_unlock(&i8259A_lock);
}
#endif
do_timer(regs);
/* /*
* In the SMP case we use the local APIC timer interrupt to do the * Here we are in the timer irq handler. We have irqs locally disabled (so we
* profiling, except when we simulate SMP mode on a uniprocessor * don't need spin_lock_irqsave()) but we don't know if the timer_bh is running
* system, in that case we have to call the local interrupt handler. * on the other CPU, so we need a lock. We also need to lock the vsyscall
* variables, because both do_timer() and us change them -arca+vojtech
*/ */
#ifndef CONFIG_X86_LOCAL_APIC
if (!user_mode(regs))
x86_do_profile(regs->rip);
#else
if (!using_apic_timer)
smp_local_timer_interrupt(regs);
#endif
/*
* If we have an externally synchronized Linux clock, then update
* CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
* called as close as possible to 500 ms before the new second starts.
*/
if ((time_status & STA_UNSYNC) == 0 &&
xtime.tv_sec > last_rtc_update + 660 &&
xtime.tv_usec >= 500000 - ((unsigned) tick) / 2 &&
xtime.tv_usec <= 500000 + ((unsigned) tick) / 2) {
if (set_rtc_mmss(xtime.tv_sec) == 0)
last_rtc_update = xtime.tv_sec;
else
last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
}
}
static int use_tsc;
/*
* This is the same as the above, except we _also_ save the current
* Time Stamp Counter value at the time of the timer interrupt, so that
* we later on can estimate the time of day more exactly.
*/
static void timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
int count;
/*
* Here we are in the timer irq handler. We just have irqs locally
* disabled but we don't know if the timer_bh is running on the other
* CPU. We need to avoid to SMP race with it. NOTE: we don' t need
* the irq version of write_lock because as just said we have irq
* locally disabled. -arca
*/
write_lock(&xtime_lock); write_lock(&xtime_lock);
vxtime_lock(); vxtime_lock();
if (use_tsc)
{ {
/* unsigned long t;
* It is important that these two operations happen almost at
* the same time. We do the RDTSC stuff first, since it's rdtscll(t);
* faster. To avoid any inconsistencies, we need interrupts hpet.offset = (t - hpet.last_tsc) * (1000000L / HZ) / hpet.ticks + hpet.offset - 1000000L / HZ;
* disabled locally. if (hpet.offset >= 1000000L / HZ)
*/ hpet.offset = 0;
hpet.ticks = min_t(long, max_t(long, (t - hpet.last_tsc) * (1000000L / HZ) / (1000000L / HZ - hpet.offset),
cpu_khz * 1000/HZ * 15 / 16), cpu_khz * 1000/HZ * 16 / 15);
hpet.last_tsc = t;
}
/* /*
* Interrupts are just disabled locally since the timer irq * Do the timer stuff.
* has the SA_INTERRUPT flag set. -arca
*/ */
/* read Pentium cycle counter */ do_timer(regs);
rdtscl(last_tsc_low);
spin_lock(&i8253_lock);
outb_p(0x00, 0x43); /* latch the count ASAP */
count = inb_p(0x40); /* read the latched count */ /*
count |= inb(0x40) << 8; * If we have an externally synchronized Linux clock, then update CMOS clock
spin_unlock(&i8253_lock); * accordingly every ~11 minutes. set_rtc_mmss() will be called in the jiffy
* closest to exactly 500 ms before the next second. If the update fails, we
* don'tcare, as it'll be updated on the next turn, and the problem (time way
* off) isn't likely to go away much sooner anyway.
*/
count = ((LATCH-1) - count) * TICK_SIZE; if ((~time_status & STA_UNSYNC) && xtime.tv_sec > rtc_update &&
delay_at_last_interrupt = (count + LATCH/2) / LATCH; abs(xtime.tv_nsec - 500000000) <= tick_nsec / 2) {
set_rtc_mmss(xtime.tv_sec);
rtc_update = xtime.tv_sec + 660;
} }
do_timer_interrupt(irq, NULL, regs);
vxtime_unlock(); vxtime_unlock();
write_unlock(&xtime_lock); write_unlock(&xtime_lock);
} }
/* not static: needed by APM */
unsigned long get_cmos_time(void) unsigned long get_cmos_time(void)
{ {
unsigned int year, mon, day, hour, min, sec; unsigned int timeout, year, mon, day, hour, min, sec;
int i; unsigned char last, this;
/*
* The Linux interpretation of the CMOS clock register contents: When the
* Update-In-Progress (UIP) flag goes from 1 to 0, the RTC registers show the
* second which has precisely just started. Waiting for this can take up to 1
* second, we timeout approximately after 2.4 seconds on a machine with
* standard 8.3 MHz ISA bus.
*/
spin_lock(&rtc_lock); spin_lock(&rtc_lock);
/* The Linux interpretation of the CMOS clock register contents:
* When the Update-In-Progress (UIP) flag goes from 1 to 0, the timeout = 1000000;
* RTC registers show the second which has precisely just started. last = this = 0;
* Let's hope other operating systems interpret the RTC the same way.
while (timeout && last && !this) {
last = this;
this = CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP;
timeout--;
}
/*
* Here we are safe to assume the registers won't change for a whole second, so
* we just go ahead and read them.
*/ */
/* read RTC exactly on falling edge of update flag */
for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */
if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
break;
for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms */
if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
break;
do { /* Isn't this overkill ? UIP above should guarantee consistency */
sec = CMOS_READ(RTC_SECONDS); sec = CMOS_READ(RTC_SECONDS);
min = CMOS_READ(RTC_MINUTES); min = CMOS_READ(RTC_MINUTES);
hour = CMOS_READ(RTC_HOURS); hour = CMOS_READ(RTC_HOURS);
day = CMOS_READ(RTC_DAY_OF_MONTH); day = CMOS_READ(RTC_DAY_OF_MONTH);
mon = CMOS_READ(RTC_MONTH); mon = CMOS_READ(RTC_MONTH);
year = CMOS_READ(RTC_YEAR); year = CMOS_READ(RTC_YEAR);
} while (sec != CMOS_READ(RTC_SECONDS));
if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) spin_unlock(&rtc_lock);
{
/*
* We know that x86-64 always uses BCD format, no need to check the config
* register.
*/
BCD_TO_BIN(sec); BCD_TO_BIN(sec);
BCD_TO_BIN(min); BCD_TO_BIN(min);
BCD_TO_BIN(hour); BCD_TO_BIN(hour);
BCD_TO_BIN(day); BCD_TO_BIN(day);
BCD_TO_BIN(mon); BCD_TO_BIN(mon);
BCD_TO_BIN(year); BCD_TO_BIN(year);
}
spin_unlock(&rtc_lock); /*
* This will work up to Dec 31, 2069.
*/
if ((year += 1900) < 1970) if ((year += 1900) < 1970)
year += 100; year += 100;
return mktime(year, mon, day, hour, min, sec); return mktime(year, mon, day, hour, min, sec);
} }
static struct irqaction irq0 = { timer_interrupt, SA_INTERRUPT, 0, "timer", NULL, NULL}; /*
* calibrate_tsc() calibrates the processor TSC in a very simple way, comparing
/* ------ Calibrate the TSC ------- * it to the HPET timer of known frequency.
* Return 2^32 * (1 / (TSC clocks per usec)) for do_fast_gettimeoffset().
* Too much 64-bit arithmetic here to do this cleanly in C, and for
* accuracy's sake we want to keep the overhead on the CTC speaker (channel 2)
* output busy loop as low as possible. We avoid reading the CTC registers
* directly because of the awkward 8-bit access mechanism of the 82C54
* device.
*/ */
#define CALIBRATE_LATCH (5 * LATCH) #define TICK_COUNT 100000000
#define CALIBRATE_TIME (5 * 1000020/HZ)
/* Could use 64bit arithmetic on x86-64, but the code is too fragile */ /*
static unsigned long __init calibrate_tsc(void) * pit_calibrate_tsc() uses the speaker output (channel 2) of
* the PIT. This is better than using the timer interrupt output,
* because we can read the value of the speaker with just one inb(),
* where we need three i/o operations for the interrupt channel.
* We count how many ticks the TSC does in 50 ms.
*/
static unsigned int __init pit_calibrate_tsc(void)
{ {
/* Set the Gate high, disable speaker */ unsigned long start, end;
outb((inb(0x61) & ~0x02) | 0x01, 0x61); unsigned long flags;
/* outb((inb(0x61) & ~0x02) | 0x01, 0x61);
* Now let's take care of CTC channel 2
*
* Set the Gate high, program CTC channel 2 for mode 0,
* (interrupt on terminal count mode), binary count,
* load 5 * LATCH count, (LSB and MSB) to begin countdown.
*/
outb(0xb0, 0x43); /* binary, mode 0, LSB/MSB, Ch 2 */
outb(CALIBRATE_LATCH & 0xff, 0x42); /* LSB of count */
outb(CALIBRATE_LATCH >> 8, 0x42); /* MSB of count */
{ local_irq_save(flags);
unsigned int startlow, starthigh; local_irq_disable();
unsigned int endlow, endhigh;
unsigned int count;
rdtsc(startlow,starthigh);
count = 0;
do {
count++;
} while ((inb(0x61) & 0x20) == 0);
rdtsc(endlow,endhigh);
last_tsc_low = endlow;
/* Error: ECTCNEVERSET */
if (count <= 1)
goto bad_ctc;
__asm__("subl %2,%0\n\t"
"sbbl %3,%1"
:"=a" (endlow), "=d" (endhigh)
:"g" (startlow), "g" (starthigh),
"0" (endlow), "1" (endhigh));
/* Error: ECPUTOOFAST */
if (endhigh)
goto bad_ctc;
/* Error: ECPUTOOSLOW */
if (endlow <= CALIBRATE_TIME)
goto bad_ctc;
__asm__("divl %2"
:"=a" (endlow), "=d" (endhigh)
:"r" (endlow), "0" (0), "1" (CALIBRATE_TIME));
return endlow;
}
/* outb(0xb0, 0x43);
* The CTC wasn't reliable: we got a hit on the very first read, outb((1193182 / (1000 / 50)) & 0xff, 0x42);
* or the CPU was so fast/slow that the quotient wouldn't fit in outb((1193182 / (1000 / 50)) >> 8, 0x42);
* 32 bits.. rdtscll(start);
*/
bad_ctc:
return 0;
}
void __init time_init(void) while ((inb(0x61) & 0x20) == 0);
{ rdtscll(end);
extern int x86_udelay_tsc;
xtime.tv_sec = get_cmos_time();
xtime.tv_usec = 0;
/* local_irq_restore(flags);
* If we have APM enabled or the CPU clock speed is variable
* (CPU stops clock on HLT or slows clock to save power)
* then the TSC timestamps may diverge by up to 1 jiffy from
* 'real time' but nothing will break.
* The most frequent case is that the CPU is "woken" from a halt
* state by the timer interrupt itself, so we get 0 error. In the
* rare cases where a driver would "wake" the CPU and request a
* timestamp, the maximum error is < 1 jiffy. But timestamps are
* still perfectly ordered.
*/
if (cpu_has_tsc) { return (end - start) / 50;
unsigned int tsc_quotient = calibrate_tsc(); }
if (tsc_quotient) {
fast_gettimeoffset_quotient = tsc_quotient;
use_tsc = 1;
/*
* We could be more selective here I suspect
* and just enable this for the next intel chips ?
*/
x86_udelay_tsc = 1;
/* report CPU clock rate in Hz. void __init pit_init(void)
* The formula is (10^6 * 2^32) / (2^32 * 1 / (clocks/us)) = {
* clock/second. Our precision is about 100 ppm. outb_p(0x34, 0x43); /* binary, mode 2, LSB/MSB, ch 0 */
*/ outb_p(LATCH & 0xff, 0x40); /* LSB */
{ unsigned int eax=0, edx=1000; outb_p(LATCH >> 8, 0x40); /* MSB */
__asm__("divl %2" }
:"=a" (cpu_khz), "=d" (edx)
:"r" (tsc_quotient),
"0" (eax), "1" (edx));
printk("Detected %u.%03u MHz processor.\n",
cpu_khz / 1000, cpu_khz % 1000);
}
}
}
setup_irq(0, &irq0); int __init time_setup(char *str)
{
hpet_report_lost_ticks = 1;
return 1;
} }
static struct device device_i8253 = { static struct irqaction irq0 = { timer_interrupt, SA_INTERRUPT, 0, "timer", NULL, NULL};
name: "i8253",
bus_id: "0040",
};
static int time_init_driverfs(void) extern void __init config_acpi_tables(void);
void __init time_init(void)
{ {
return register_sys_device(&device_i8253); xtime.tv_sec = get_cmos_time();
xtime.tv_nsec = 0;
pit_init();
printk(KERN_INFO "time.c: Using 1.1931816 MHz PIT timer.\n");
setup_irq(0, &irq0);
cpu_khz = pit_calibrate_tsc();
printk(KERN_INFO "time.c: Detected %d.%03d MHz processor.\n",
cpu_khz / 1000, cpu_khz % 1000);
hpet.ticks = cpu_khz * (1000 / HZ);
rdtscll(hpet.last_tsc);
} }
__initcall(time_init_driverfs); __setup("report_lost_ticks", time_setup);
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
#define __vsyscall(nr) __attribute__ ((unused,__section__(".vsyscall_" #nr))) #define __vsyscall(nr) __attribute__ ((unused,__section__(".vsyscall_" #nr)))
//#define NO_VSYSCALL 1 #define NO_VSYSCALL 1
#ifdef NO_VSYSCALL #ifdef NO_VSYSCALL
#include <asm/unistd.h> #include <asm/unistd.h>
...@@ -71,51 +71,27 @@ static inline void timeval_normalize(struct timeval * tv) ...@@ -71,51 +71,27 @@ static inline void timeval_normalize(struct timeval * tv)
long __vxtime_sequence[2] __section_vxtime_sequence; long __vxtime_sequence[2] __section_vxtime_sequence;
static inline void do_vgettimeofday(struct timeval * tv) static inline void do_vgettimeofday(struct timeval * tv)
{ {
long sequence; long sequence, t;
unsigned long usec, sec; unsigned long sec, usec;
do { do {
unsigned long eax, edx;
sequence = __vxtime_sequence[1]; sequence = __vxtime_sequence[1];
rmb(); rmb();
/* Read the Time Stamp Counter */ rdtscll(t);
rdtsc(eax,edx);
/* .. relative to previous jiffy (32 bits is enough) */
eax -= __last_tsc_low; /* tsc_low delta */
/*
* Time offset = (tsc_low delta) * fast_gettimeoffset_quotient
* = (tsc_low delta) * (usecs_per_clock)
* = (tsc_low delta) * (usecs_per_jiffy / clocks_per_jiffy)
*
* Using a mull instead of a divl saves up to 31 clock cycles
* in the critical path.
*/
edx = (eax*__fast_gettimeoffset_quotient) >> 32;
/* our adjusted time offset in microseconds */
usec = __delay_at_last_interrupt + edx;
{
unsigned long lost = __jiffies - __wall_jiffies;
if (lost)
usec += lost * (1000000 / HZ);
}
sec = __xtime.tv_sec; sec = __xtime.tv_sec;
usec += __xtime.tv_usec; usec = __xtime.tv_usec +
(__jiffies - __wall_jiffies) * (1000000 / HZ) +
(t - __hpet.last_tsc) * (1000000 / HZ) / __hpet.ticks + __hpet.offset;
rmb(); rmb();
} while (sequence != __vxtime_sequence[0]); } while (sequence != __vxtime_sequence[0]);
tv->tv_sec = sec; tv->tv_sec = sec + usec / 1000000;
tv->tv_usec = usec; tv->tv_usec = usec % 1000000;
timeval_normalize(tv);
} }
static inline void do_get_tz(struct timezone * tz) static inline void do_get_tz(struct timezone * tz)
......
...@@ -36,16 +36,14 @@ extern cycles_t cacheflush_time; ...@@ -36,16 +36,14 @@ extern cycles_t cacheflush_time;
static inline cycles_t get_cycles (void) static inline cycles_t get_cycles (void)
{ {
#ifndef CONFIG_X86_TSC
return 0;
#else
unsigned long long ret; unsigned long long ret;
rdtscll(ret); rdtscll(ret);
return ret; return ret;
#endif
} }
extern unsigned int cpu_khz; extern unsigned int cpu_khz;
extern struct hpet_data hpet;
#endif #endif
#ifndef _ASM_X86_64_VSYSCALL_H_ #ifndef _ASM_X86_64_VSYSCALL_H_
#define _ASM_X86_64_VSYSCALL_H_ #define _ASM_X86_64_VSYSCALL_H_
#include <linux/time.h>
enum vsyscall_num { enum vsyscall_num {
__NR_vgettimeofday, __NR_vgettimeofday,
__NR_vtime, __NR_vtime,
...@@ -13,32 +15,39 @@ enum vsyscall_num { ...@@ -13,32 +15,39 @@ enum vsyscall_num {
#ifdef __KERNEL__ #ifdef __KERNEL__
#define __section_last_tsc_low __attribute__ ((unused, __section__ (".last_tsc_low"))) #define __section_hpet __attribute__ ((unused, __section__ (".hpet"), aligned(16)))
#define __section_delay_at_last_interrupt __attribute__ ((unused, __section__ (".delay_at_last_interrupt"))) #define __section_wall_jiffies __attribute__ ((unused, __section__ (".wall_jiffies"), aligned(16)))
#define __section_fast_gettimeoffset_quotient __attribute__ ((unused, __section__ (".fast_gettimeoffset_quotient"))) #define __section_jiffies __attribute__ ((unused, __section__ (".jiffies"), aligned(16)))
#define __section_wall_jiffies __attribute__ ((unused, __section__ (".wall_jiffies"))) #define __section_sys_tz __attribute__ ((unused, __section__ (".sys_tz"), aligned(16)))
#define __section_jiffies __attribute__ ((unused, __section__ (".jiffies"))) #define __section_xtime __attribute__ ((unused, __section__ (".xtime"), aligned(16)))
#define __section_sys_tz __attribute__ ((unused, __section__ (".sys_tz"))) #define __section_vxtime_sequence __attribute__ ((unused, __section__ (".vxtime_sequence"), aligned(16)))
#define __section_xtime __attribute__ ((unused, __section__ (".xtime")))
#define __section_vxtime_sequence __attribute__ ((unused, __section__ (".vxtime_sequence"))) struct hpet_data {
long address; /* base address */
unsigned long hz; /* HPET clocks / sec */
int trigger; /* value at last interrupt */
int last;
int offset;
unsigned long last_tsc;
long ticks;
};
#define hpet_readl(a) readl(fix_to_virt(FIX_HPET_BASE) + a)
#define hpet_writel(d,a) writel(d, fix_to_virt(FIX_HPET_BASE) + a)
/* vsyscall space (readonly) */ /* vsyscall space (readonly) */
extern long __vxtime_sequence[2]; extern long __vxtime_sequence[2];
extern int __delay_at_last_interrupt; extern struct hpet_data __hpet;
extern unsigned int __last_tsc_low; extern struct timespec __xtime;
extern unsigned int __fast_gettimeoffset_quotient;
extern struct timeval __xtime;
extern volatile unsigned long __jiffies; extern volatile unsigned long __jiffies;
extern unsigned long __wall_jiffies; extern unsigned long __wall_jiffies;
extern struct timezone __sys_tz; extern struct timezone __sys_tz;
/* kernel space (writeable) */ /* kernel space (writeable) */
extern unsigned long last_tsc_low; extern long vxtime_sequence[2];
extern int delay_at_last_interrupt; extern struct hpet_data hpet;
extern unsigned int fast_gettimeoffset_quotient;
extern unsigned long wall_jiffies; extern unsigned long wall_jiffies;
extern struct timezone sys_tz; extern struct timezone sys_tz;
extern long vxtime_sequence[2];
#define vxtime_lock() do { vxtime_sequence[0]++; wmb(); } while(0) #define vxtime_lock() do { vxtime_sequence[0]++; wmb(); } while(0)
#define vxtime_unlock() do { wmb(); vxtime_sequence[1]++; } while (0) #define vxtime_unlock() do { wmb(); vxtime_sequence[1]++; } while (0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment