[timer] Formalise the timer API

We now have two implementations for the timer API: one using the
time-of-day counter at 40:70 and one using RDTSC.  Both make use of
timer2_udelay().
This commit is contained in:
Michael Brown
2008-10-12 19:56:52 +01:00
parent e6f276ece3
commit 16f1e35775
22 changed files with 386 additions and 284 deletions

View File

@@ -1,57 +0,0 @@
/*
* Etherboot routines for PCBIOS firmware.
*
* Body of routines taken from old pcbios.S
*/
#include <gpxe/init.h>
#include <gpxe/timer.h>
#include <stdio.h>
#include <realmode.h>
#include <bios.h>
#include <bits/timer2.h>
/* A bit faster actually, but we don't care. */
#define TIMER2_TICKS_PER_SEC 18
/*
* Use direct memory access to BIOS variables, longword 0040:006C (ticks
* today) and byte 0040:0070 (midnight crossover flag) instead of calling
* timeofday BIOS interrupt.
*/
static tick_t bios_currticks ( void ) {
static int days = 0;
uint32_t ticks;
uint8_t midnight;
/* Re-enable interrupts so that the timer interrupt can occur */
__asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
"nop\n\t"
"nop\n\t"
"cli\n\t" ) : : );
get_real ( ticks, BDA_SEG, 0x006c );
get_real ( midnight, BDA_SEG, 0x0070 );
if ( midnight ) {
midnight = 0;
put_real ( midnight, BDA_SEG, 0x0070 );
days += 0x1800b0;
}
return ( (days + ticks) * (USECS_IN_SEC / TIMER2_TICKS_PER_SEC) );
}
static int bios_ts_init(void)
{
DBG("BIOS timer installed\n");
return 0;
}
struct timer bios_ts __timer ( 02 ) = {
.init = bios_ts_init,
.udelay = i386_timer2_udelay,
.currticks = bios_currticks,
};

View File

@@ -1,69 +0,0 @@
#include <gpxe/init.h>
#include <gpxe/timer.h>
#include <errno.h>
#include <stdio.h>
#include <bits/cpu.h>
#include <bits/timer2.h>
#include <gpxe/io.h>
#define rdtsc(low,high) \
__asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
#define rdtscll(val) \
__asm__ __volatile__ ("rdtsc" : "=A" (val))
/* Measure how many clocks we get in one microsecond */
static inline uint64_t calibrate_tsc(void)
{
uint64_t rdtsc_start;
uint64_t rdtsc_end;
rdtscll(rdtsc_start);
i386_timer2_udelay(USECS_IN_MSEC);
rdtscll(rdtsc_end);
return (rdtsc_end - rdtsc_start) / USECS_IN_MSEC;
}
static uint32_t clocks_per_usec = 0;
/* We measure time in microseconds. */
static tick_t rdtsc_currticks(void)
{
uint64_t clocks;
/* Read the Time Stamp Counter */
rdtscll(clocks);
return clocks / clocks_per_usec;
}
static int rdtsc_ts_init(void)
{
struct cpuinfo_x86 cpu_info;
get_cpuinfo(&cpu_info);
if (cpu_info.features & X86_FEATURE_TSC) {
clocks_per_usec= calibrate_tsc();
if (clocks_per_usec) {
DBG("RDTSC ticksource installed. CPU running at %ld Mhz\n",
clocks_per_usec);
return 0;
}
}
DBG("RDTSC ticksource not available on this machine.\n");
return -ENODEV;
}
struct timer rdtsc_ts __timer (01) = {
.init = rdtsc_ts_init,
.udelay = generic_currticks_udelay,
.currticks = rdtsc_currticks,
};