iPXE
rdtsc_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 /** @file
00027  *
00028  * RDTSC timer
00029  *
00030  */
00031 
00032 #include <string.h>
00033 #include <errno.h>
00034 #include <ipxe/timer.h>
00035 #include <ipxe/cpuid.h>
00036 #include <ipxe/pit8254.h>
00037 
00038 /** Number of microseconds to use for TSC calibration */
00039 #define TSC_CALIBRATE_US 1024
00040 
00041 /** TSC increment per microsecond */
00042 static unsigned long tsc_per_us;
00043 
00044 /** Minimum resolution for scaled TSC timer */
00045 #define TSC_SCALED_HZ 32
00046 
00047 /** TSC scale (expressed as a bit shift)
00048  *
00049  * We use this to avoid the need for 64-bit divsion on 32-bit systems.
00050  */
00051 static unsigned int tsc_scale;
00052 
00053 /** Number of timer ticks per scaled TSC increment */
00054 static unsigned long ticks_per_scaled_tsc;
00055 
00056 /** Colour for debug messages */
00057 #define colour &tsc_per_us
00058 
00059 /**
00060  * Get raw TSC value
00061  *
00062  * @ret tsc             Raw TSC value
00063  */
00064 static inline __always_inline unsigned long rdtsc_raw ( void ) {
00065         unsigned long raw;
00066 
00067         __asm__ __volatile__ ( "rdtsc\n\t" : "=a" ( raw ) : : "edx" );
00068         return raw;
00069 }
00070 
00071 /**
00072  * Get TSC value, shifted to avoid rollover within a realistic timescale
00073  *
00074  * @ret tsc             Scaled TSC value
00075  */
00076 static inline __always_inline unsigned long rdtsc_scaled ( void ) {
00077         unsigned long scaled;
00078 
00079         __asm__ __volatile__ ( "rdtsc\n\t"
00080                                "shrdl %b1, %%edx, %%eax\n\t"
00081                                : "=a" ( scaled ) : "c" ( tsc_scale ) : "edx" );
00082         return scaled;
00083 }
00084 
00085 /**
00086  * Get current system time in ticks
00087  *
00088  * @ret ticks           Current time, in ticks
00089  */
00090 static unsigned long rdtsc_currticks ( void ) {
00091         unsigned long scaled;
00092 
00093         scaled = rdtsc_scaled();
00094         return ( scaled * ticks_per_scaled_tsc );
00095 }
00096 
00097 /**
00098  * Delay for a fixed number of microseconds
00099  *
00100  * @v usecs             Number of microseconds for which to delay
00101  */
00102 static void rdtsc_udelay ( unsigned long usecs ) {
00103         unsigned long start;
00104         unsigned long elapsed;
00105         unsigned long threshold;
00106 
00107         start = rdtsc_raw();
00108         threshold = ( usecs * tsc_per_us );
00109         do {
00110                 elapsed = ( rdtsc_raw() - start );
00111         } while ( elapsed < threshold );
00112 }
00113 
00114 /**
00115  * Probe RDTSC timer
00116  *
00117  * @ret rc              Return status code
00118  */
00119 static int rdtsc_probe ( void ) {
00120         unsigned long before;
00121         unsigned long after;
00122         unsigned long elapsed;
00123         uint32_t apm;
00124         uint32_t discard_a;
00125         uint32_t discard_b;
00126         uint32_t discard_c;
00127         int rc;
00128 
00129         /* Check that TSC is invariant */
00130         if ( ( rc = cpuid_supported ( CPUID_APM ) ) != 0 ) {
00131                 DBGC ( colour, "RDTSC cannot determine APM features: %s\n",
00132                        strerror ( rc ) );
00133                 return rc;
00134         }
00135         cpuid ( CPUID_APM, 0, &discard_a, &discard_b, &discard_c, &apm );
00136         if ( ! ( apm & CPUID_APM_EDX_TSC_INVARIANT ) ) {
00137                 DBGC ( colour, "RDTSC has non-invariant TSC (%#08x)\n",
00138                        apm );
00139                 return -ENOTTY;
00140         }
00141 
00142         /* Calibrate udelay() timer via 8254 PIT */
00143         before = rdtsc_raw();
00144         pit8254_udelay ( TSC_CALIBRATE_US );
00145         after = rdtsc_raw();
00146         elapsed = ( after - before );
00147         tsc_per_us = ( elapsed / TSC_CALIBRATE_US );
00148         if ( ! tsc_per_us ) {
00149                 DBGC ( colour, "RDTSC has zero TSC per microsecond\n" );
00150                 return -EIO;
00151         }
00152 
00153         /* Calibrate currticks() scaling factor */
00154         tsc_scale = 31;
00155         ticks_per_scaled_tsc = ( ( 1UL << tsc_scale ) /
00156                                  ( tsc_per_us * ( 1000000 / TICKS_PER_SEC ) ) );
00157         while ( ticks_per_scaled_tsc > ( TICKS_PER_SEC / TSC_SCALED_HZ ) ) {
00158                 tsc_scale--;
00159                 ticks_per_scaled_tsc >>= 1;
00160         }
00161         DBGC ( colour, "RDTSC has %ld tsc per us, %ld ticks per 2^%d tsc\n",
00162                tsc_per_us, ticks_per_scaled_tsc, tsc_scale );
00163         if ( ! ticks_per_scaled_tsc ) {
00164                 DBGC ( colour, "RDTSC has zero ticks per TSC\n" );
00165                 return -EIO;
00166         }
00167 
00168         return 0;
00169 }
00170 
00171 /** RDTSC timer */
00172 struct timer rdtsc_timer __timer ( TIMER_PREFERRED ) = {
00173         .name = "rdtsc",
00174         .probe = rdtsc_probe,
00175         .currticks = rdtsc_currticks,
00176         .udelay = rdtsc_udelay,
00177 };