These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / interface / efi / efi_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 #include <string.h>
27 #include <errno.h>
28 #include <limits.h>
29 #include <assert.h>
30 #include <unistd.h>
31 #include <ipxe/timer.h>
32 #include <ipxe/efi/efi.h>
33 #include <ipxe/efi/Protocol/Cpu.h>
34
35 /** @file
36  *
37  * iPXE timer API for EFI
38  *
39  */
40
41 /** Scale factor to apply to CPU timer 0
42  *
43  * The timer is scaled down in order to ensure that reasonable values
44  * for "number of ticks" don't exceed the size of an unsigned long.
45  */
46 #define EFI_TIMER0_SHIFT 12
47
48 /** Calibration time */
49 #define EFI_CALIBRATE_DELAY_MS 1
50
51 /** CPU protocol */
52 static EFI_CPU_ARCH_PROTOCOL *cpu_arch;
53 EFI_REQUIRE_PROTOCOL ( EFI_CPU_ARCH_PROTOCOL, &cpu_arch );
54
55 /**
56  * Delay for a fixed number of microseconds
57  *
58  * @v usecs             Number of microseconds for which to delay
59  */
60 static void efi_udelay ( unsigned long usecs ) {
61         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
62         EFI_STATUS efirc;
63         int rc;
64
65         if ( ( efirc = bs->Stall ( usecs ) ) != 0 ) {
66                 rc = -EEFI ( efirc );
67                 DBG ( "EFI could not delay for %ldus: %s\n",
68                       usecs, strerror ( rc ) );
69                 /* Probably screwed */
70         }
71 }
72
73 /**
74  * Get current system time in ticks
75  *
76  * @ret ticks           Current time, in ticks
77  */
78 static unsigned long efi_currticks ( void ) {
79         UINT64 time;
80         EFI_STATUS efirc;
81         int rc;
82
83         /* Read CPU timer 0 (TSC) */
84         if ( ( efirc = cpu_arch->GetTimerValue ( cpu_arch, 0, &time,
85                                                  NULL ) ) != 0 ) {
86                 rc = -EEFI ( efirc );
87                 DBG ( "EFI could not read CPU timer: %s\n", strerror ( rc ) );
88                 /* Probably screwed */
89                 return -1UL;
90         }
91
92         return ( time >> EFI_TIMER0_SHIFT );
93 }
94
95 /**
96  * Get number of ticks per second
97  *
98  * @ret ticks_per_sec   Number of ticks per second
99  */
100 static unsigned long efi_ticks_per_sec ( void ) {
101         static unsigned long ticks_per_sec = 0;
102
103         /* Calibrate timer, if necessary.  EFI does nominally provide
104          * the timer speed via the (optional) TimerPeriod parameter to
105          * the GetTimerValue() call, but it gets the speed slightly
106          * wrong.  By up to three orders of magnitude.  Not helpful.
107          */
108         if ( ! ticks_per_sec ) {
109                 unsigned long start;
110                 unsigned long elapsed;
111
112                 DBG ( "Calibrating EFI timer with a %d ms delay\n",
113                       EFI_CALIBRATE_DELAY_MS );
114                 start = currticks();
115                 mdelay ( EFI_CALIBRATE_DELAY_MS );
116                 elapsed = ( currticks() - start );
117                 ticks_per_sec = ( elapsed * ( 1000 / EFI_CALIBRATE_DELAY_MS ));
118                 DBG ( "EFI CPU timer calibrated at %ld ticks in %d ms (%ld "
119                       "ticks/sec)\n", elapsed, EFI_CALIBRATE_DELAY_MS,
120                       ticks_per_sec );
121         }
122
123         return ticks_per_sec;
124 }
125
126 PROVIDE_TIMER ( efi, udelay, efi_udelay );
127 PROVIDE_TIMER ( efi, currticks, efi_currticks );
128 PROVIDE_TIMER ( efi, ticks_per_sec, efi_ticks_per_sec );