iPXE
timer.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <string.h>
00027 #include <ipxe/process.h>
00028 #include <ipxe/console.h>
00029 #include <ipxe/keys.h>
00030 #include <ipxe/nap.h>
00031 #include <ipxe/init.h>
00032 #include <ipxe/timer.h>
00033 
00034 /** Current timer */
00035 static struct timer *timer;
00036 
00037 /**
00038  * Get current system time in ticks
00039  *
00040  * @ret ticks           Current time, in ticks
00041  */
00042 unsigned long currticks ( void ) {
00043 
00044         /* Guard against use during early initialisation */
00045         if ( ! timer ) {
00046                 DBGC ( &timer, "TIMER currticks() called before initialisation "
00047                        "from %p\n", __builtin_return_address ( 0 ) );
00048                 return 0;
00049         }
00050 
00051         /* Use selected timer */
00052         return timer->currticks();
00053 }
00054 
00055 /**
00056  * Delay for a fixed number of microseconds
00057  *
00058  * @v usecs             Number of microseconds for which to delay
00059  */
00060 void udelay ( unsigned long usecs ) {
00061 
00062         /* Guard against use during early initialisation */
00063         if ( ! timer ) {
00064                 DBGC ( &timer, "TIMER udelay() called before initialisation "
00065                        "from %p\n", __builtin_return_address ( 0 ) );
00066                 return;
00067         }
00068 
00069         /* Use selected timer */
00070         timer->udelay ( usecs );
00071 }
00072 
00073 /**
00074  * Delay for a fixed number of milliseconds
00075  *
00076  * @v msecs             Number of milliseconds for which to delay
00077  */
00078 void mdelay ( unsigned long msecs ) {
00079 
00080         /* Guard against use during early initialisation */
00081         if ( ! timer ) {
00082                 DBGC ( &timer, "TIMER mdelay() called before initialisation "
00083                        "from %p\n", __builtin_return_address ( 0 ) );
00084                 return;
00085         }
00086 
00087         /* Delay for specified number of milliseconds */
00088         while ( msecs-- )
00089                 udelay ( 1000 );
00090 }
00091 
00092 /**
00093  * Sleep (possibly interruptibly) for a fixed number of seconds
00094  *
00095  * @v secs              Number of seconds for which to delay
00096  * @v interrupted       Interrupt checking method, or NULL
00097  * @ret secs            Number of seconds remaining, if interrupted
00098  */
00099 static unsigned int sleep_interruptible ( unsigned int secs,
00100                                           int ( * interrupted ) ( void ) ) {
00101         unsigned long start = currticks();
00102         unsigned long now;
00103 
00104         for ( ; secs ; secs-- ) {
00105                 while ( ( ( now = currticks() ) - start ) < TICKS_PER_SEC ) {
00106                         step();
00107                         if ( interrupted && interrupted() )
00108                                 return secs;
00109                         cpu_nap();
00110                 }
00111                 start = now;
00112         }
00113 
00114         return 0;
00115 }
00116 
00117 /**
00118  * Check if sleep has been interrupted by keypress
00119  *
00120  * @ret interrupted     Sleep has been interrupted
00121  */
00122 static int keypress_interrupted ( void ) {
00123 
00124         return ( iskey() && ( getchar() == CTRL_C ) );
00125 }
00126 
00127 /**
00128  * Sleep (interruptibly) for a fixed number of seconds
00129  *
00130  * @v secs              Number of seconds for which to delay
00131  * @ret secs            Number of seconds remaining, if interrupted
00132  */
00133 unsigned int sleep ( unsigned int secs ) {
00134 
00135         return sleep_interruptible ( secs, keypress_interrupted );
00136 }
00137 
00138 /**
00139  * Sleep (uninterruptibly) for a fixed number of seconds
00140  *
00141  * @v secs              Number of seconds for which to delay
00142  */
00143 void sleep_fixed ( unsigned int secs ) {
00144 
00145         sleep_interruptible ( secs, NULL );
00146 }
00147 
00148 /**
00149  * Find a working timer
00150  *
00151  */
00152 static void timer_probe ( void ) {
00153         int rc;
00154 
00155         /* Use first working timer */
00156         for_each_table_entry ( timer, TIMERS ) {
00157                 if ( ( timer->probe == NULL ) ||
00158                      ( ( rc = timer->probe() ) == 0 ) ) {
00159                         DBGC ( &timer, "TIMER using %s\n", timer->name );
00160                         return;
00161                 }
00162                 DBGC ( &timer, "TIMER could not initialise %s: %s\n",
00163                        timer->name, strerror ( rc ) );
00164         }
00165 
00166         /* This is a fatal error */
00167         DBGC ( &timer, "TIMER found no working timers!\n" );
00168         while ( 1 ) {}
00169 }
00170 
00171 /** Timer initialisation function */
00172 struct init_fn timer_init_fn __init_fn ( INIT_EARLY ) = {
00173         .initialise = timer_probe,
00174 };
00175 
00176 /* Drag in timer configuration */
00177 REQUIRING_SYMBOL ( timer_init_fn );
00178 REQUIRE_OBJECT ( config_timer );