[Timers] Initialise timers on first use

Timers are sometimes required before the call to initialise(), so we
cannot rely on initialise() to set up the timers before use.

Also fix a potential integer overflow issue in generic_currticks_udelay()
This commit is contained in:
Michael Brown
2008-03-18 15:23:10 +00:00
parent 2652abdc5f
commit 070cbebc26
2 changed files with 26 additions and 27 deletions

View File

@@ -20,7 +20,6 @@
#include <stddef.h> #include <stddef.h>
#include <assert.h> #include <assert.h>
#include <gpxe/init.h>
#include <gpxe/timer.h> #include <gpxe/timer.h>
static struct timer ts_table[0] static struct timer ts_table[0]
@@ -28,34 +27,39 @@ static struct timer ts_table[0]
static struct timer ts_table_end[0] static struct timer ts_table_end[0]
__table_end ( struct timer, timers ); __table_end ( struct timer, timers );
static struct timer *used_ts = NULL;
/* /*
* This function may be used in custom timer driver. * This function may be used in custom timer driver.
* *
* This udelay implementation works well if you've got a * This udelay implementation works well if you've got a
* fast currticks(). * fast currticks().
*/ */
void generic_currticks_udelay(unsigned int usecs) void generic_currticks_udelay ( unsigned int usecs ) {
{ tick_t start;
tick_t t; tick_t elapsed;
t = currticks(); start = currticks();
while (t + usecs > currticks()) do {
; /* xxx: Relax the cpu some way. */ /* xxx: Relax the cpu some way. */
elapsed = ( currticks() - start );
} while ( elapsed < usecs );
} }
/**
* Identify timer source
*
* @ret timer Timer source
*/
static struct timer * timer ( void ) {
static struct timer *ts = NULL;
static void timer_init(void) /* If we have a timer, use it */
{ if ( ts )
struct timer *ts; return ts;
for (ts = ts_table; ts < ts_table_end; ts++) { /* Scan for a usable timer */
if ( ts->init() == 0 ) { for ( ts = ts_table ; ts < ts_table_end ; ts++ ) {
used_ts = ts; if ( ts->init() == 0 )
return; return ts;
}
} }
/* No timer found; we cannot continue */ /* No timer found; we cannot continue */
@@ -63,10 +67,6 @@ static void timer_init(void)
while ( 1 ) {}; while ( 1 ) {};
} }
struct init_fn ts_init_fn __init_fn ( INIT_NORMAL ) = {
.initialise = timer_init,
};
/** /**
* Read current time * Read current time
* *
@@ -74,9 +74,8 @@ struct init_fn ts_init_fn __init_fn ( INIT_NORMAL ) = {
*/ */
tick_t currticks ( void ) { tick_t currticks ( void ) {
tick_t ct; tick_t ct;
assert(used_ts);
ct = used_ts->currticks(); ct = timer()->currticks();
DBG ( "currticks: %ld.%06ld seconds\n", DBG ( "currticks: %ld.%06ld seconds\n",
ct / USECS_IN_SEC, ct % USECS_IN_SEC ); ct / USECS_IN_SEC, ct % USECS_IN_SEC );
@@ -89,8 +88,7 @@ tick_t currticks ( void ) {
* @v usecs Time to delay, in microseconds * @v usecs Time to delay, in microseconds
*/ */
void udelay ( unsigned int usecs ) { void udelay ( unsigned int usecs ) {
assert(used_ts); timer()->udelay ( usecs );
used_ts->udelay ( usecs );
} }
/** /**

View File

@@ -2,6 +2,7 @@
#define GPXE_TIMER_H #define GPXE_TIMER_H
#include <stddef.h> #include <stddef.h>
#include <gpxe/tables.h>
typedef unsigned long tick_t; typedef unsigned long tick_t;