These changes are the raw update to qemu-2.6.
[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  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27  *
28  * RDTSC timer
29  *
30  */
31
32 #include <assert.h>
33 #include <ipxe/timer.h>
34 #include <ipxe/pit8254.h>
35
36 /**
37  * Number of TSC ticks per microsecond
38  *
39  * This is calibrated on the first use of the timer.
40  */
41 static unsigned long rdtsc_ticks_per_usec;
42
43 /**
44  * Delay for a fixed number of microseconds
45  *
46  * @v usecs             Number of microseconds for which to delay
47  */
48 static void rdtsc_udelay ( unsigned long usecs ) {
49         unsigned long start;
50         unsigned long elapsed;
51
52         /* Sanity guard, since we may divide by this */
53         if ( ! usecs )
54                 usecs = 1;
55
56         start = currticks();
57         if ( rdtsc_ticks_per_usec ) {
58                 /* Already calibrated; busy-wait until done */
59                 do {
60                         elapsed = ( currticks() - start );
61                 } while ( elapsed < ( usecs * rdtsc_ticks_per_usec ) );
62         } else {
63                 /* Not yet calibrated; use 8254 PIT and calibrate
64                  * based on result.
65                  */
66                 pit8254_udelay ( usecs );
67                 elapsed = ( currticks() - start );
68                 rdtsc_ticks_per_usec = ( elapsed / usecs );
69                 DBG ( "RDTSC timer calibrated: %ld ticks in %ld usecs "
70                       "(%ld MHz)\n", elapsed, usecs,
71                       ( rdtsc_ticks_per_usec << TSC_SHIFT ) );
72         }
73 }
74
75 /**
76  * Get number of ticks per second
77  *
78  * @ret ticks_per_sec   Number of ticks per second
79  */
80 static unsigned long rdtsc_ticks_per_sec ( void ) {
81
82         /* Calibrate timer, if not already done */
83         if ( ! rdtsc_ticks_per_usec )
84                 udelay ( 1 );
85
86         /* Sanity check */
87         assert ( rdtsc_ticks_per_usec != 0 );
88
89         return ( rdtsc_ticks_per_usec * 1000 * 1000 );
90 }
91
92 PROVIDE_TIMER ( rdtsc, udelay, rdtsc_udelay );
93 PROVIDE_TIMER_INLINE ( rdtsc, currticks );
94 PROVIDE_TIMER ( rdtsc, ticks_per_sec, rdtsc_ticks_per_sec );