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