Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / core / rdtsc_timer.c
1 /*
2  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23  *
24  * RDTSC timer
25  *
26  */
27
28 #include <assert.h>
29 #include <ipxe/timer.h>
30 #include <ipxe/timer2.h>
31
32 /**
33  * Number of TSC ticks per microsecond
34  *
35  * This is calibrated on the first use of the timer.
36  */
37 static unsigned long rdtsc_ticks_per_usec;
38
39 /**
40  * Delay for a fixed number of microseconds
41  *
42  * @v usecs             Number of microseconds for which to delay
43  */
44 static void rdtsc_udelay ( unsigned long usecs ) {
45         unsigned long start;
46         unsigned long elapsed;
47
48         /* Sanity guard, since we may divide by this */
49         if ( ! usecs )
50                 usecs = 1;
51
52         start = currticks();
53         if ( rdtsc_ticks_per_usec ) {
54                 /* Already calibrated; busy-wait until done */
55                 do {
56                         elapsed = ( currticks() - start );
57                 } while ( elapsed < ( usecs * rdtsc_ticks_per_usec ) );
58         } else {
59                 /* Not yet calibrated; use timer2 and calibrate
60                  * based on result.
61                  */
62                 timer2_udelay ( usecs );
63                 elapsed = ( currticks() - start );
64                 rdtsc_ticks_per_usec = ( elapsed / usecs );
65                 DBG ( "RDTSC timer calibrated: %ld ticks in %ld usecs "
66                       "(%ld MHz)\n", elapsed, usecs,
67                       ( rdtsc_ticks_per_usec << TSC_SHIFT ) );
68         }
69 }
70
71 /**
72  * Get number of ticks per second
73  *
74  * @ret ticks_per_sec   Number of ticks per second
75  */
76 static unsigned long rdtsc_ticks_per_sec ( void ) {
77
78         /* Calibrate timer, if not already done */
79         if ( ! rdtsc_ticks_per_usec )
80                 udelay ( 1 );
81
82         /* Sanity check */
83         assert ( rdtsc_ticks_per_usec != 0 );
84
85         return ( rdtsc_ticks_per_usec * 1000 * 1000 );
86 }
87
88 PROVIDE_TIMER ( rdtsc, udelay, rdtsc_udelay );
89 PROVIDE_TIMER_INLINE ( rdtsc, currticks );
90 PROVIDE_TIMER ( rdtsc, ticks_per_sec, rdtsc_ticks_per_sec );