2 A non-functional instance of the Timer Library.
4 Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/BaseLib.h>
17 #include <Library/TimerLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/EmuThunkLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21 #include <Library/UefiLib.h>
23 #include <Protocol/Timer.h>
26 STATIC UINT64 gTimerPeriod
= 0;
27 STATIC EFI_TIMER_ARCH_PROTOCOL
*gTimerAp
= NULL
;
28 STATIC EFI_EVENT gTimerEvent
= NULL
;
29 STATIC VOID
*gRegistration
= NULL
;
33 RegisterTimerArchProtocol (
40 Status
= gBS
->LocateProtocol (&gEfiTimerArchProtocolGuid
, NULL
, (VOID
**)&gTimerAp
);
41 if (!EFI_ERROR (Status
)) {
42 Status
= gTimerAp
->GetTimerPeriod (gTimerAp
, &gTimerPeriod
);
43 ASSERT_EFI_ERROR (Status
);
45 // Convert to Nanoseconds.
46 gTimerPeriod
= MultU64x32 (gTimerPeriod
, 100);
48 if (gTimerEvent
== NULL
) {
49 Status
= gBS
->CreateEvent (EVT_TIMER
, 0, NULL
, NULL
, (VOID
**)&gTimerEvent
);
50 ASSERT_EFI_ERROR (Status
);
58 Stalls the CPU for at least the given number of microseconds.
60 Stalls the CPU for the number of microseconds specified by MicroSeconds.
62 @param MicroSeconds The minimum number of microseconds to delay.
64 @return The value of MicroSeconds inputted.
73 return NanoSecondDelay (MicroSeconds
* 1000);
78 Stalls the CPU for at least the given number of nanoseconds.
80 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
82 @param NanoSeconds The minimum number of nanoseconds to delay.
84 @return The value of NanoSeconds inputted.
94 UINT64 HundredNanoseconds
;
97 if ((gTimerPeriod
!= 0) &&
98 ((UINT64
)NanoSeconds
> gTimerPeriod
) &&
99 (EfiGetCurrentTpl () == TPL_APPLICATION
)) {
101 // This stall is long, so use gBS->WaitForEvent () to yield CPU to DXE Core
104 HundredNanoseconds
= DivU64x32 (NanoSeconds
, 100);
105 Status
= gBS
->SetTimer (gTimerEvent
, TimerRelative
, HundredNanoseconds
);
106 ASSERT_EFI_ERROR (Status
);
108 Status
= gBS
->WaitForEvent (sizeof (gTimerEvent
)/sizeof (EFI_EVENT
), &gTimerEvent
, &Index
);
109 ASSERT_EFI_ERROR (Status
);
112 gEmuThunk
->Sleep (NanoSeconds
);
119 Retrieves the current value of a 64-bit free running performance counter.
121 The counter can either count up by 1 or count down by 1. If the physical
122 performance counter counts by a larger increment, then the counter values
123 must be translated. The properties of the counter can be retrieved from
124 GetPerformanceCounterProperties().
126 @return The current value of the free running performance counter.
131 GetPerformanceCounter (
135 return gEmuThunk
->QueryPerformanceCounter ();
139 Retrieves the 64-bit frequency in Hz and the range of performance counter
142 If StartValue is not NULL, then the value that the performance counter starts
143 with immediately after is it rolls over is returned in StartValue. If
144 EndValue is not NULL, then the value that the performance counter end with
145 immediately before it rolls over is returned in EndValue. The 64-bit
146 frequency of the performance counter in Hz is always returned. If StartValue
147 is less than EndValue, then the performance counter counts up. If StartValue
148 is greater than EndValue, then the performance counter counts down. For
149 example, a 64-bit free running counter that counts up would have a StartValue
150 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
151 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
153 @param StartValue The value the performance counter starts with when it
155 @param EndValue The value that the performance counter ends with before
158 @return The frequency in Hz.
163 GetPerformanceCounterProperties (
164 OUT UINT64
*StartValue
, OPTIONAL
165 OUT UINT64
*EndValue OPTIONAL
169 if (StartValue
!= NULL
) {
172 if (EndValue
!= NULL
) {
173 *EndValue
= (UINT64
)-1LL;
176 return gEmuThunk
->QueryPerformanceFrequency ();
181 Register for the Timer AP protocol.
183 @param ImageHandle The firmware allocated handle for the EFI image.
184 @param SystemTable A pointer to the EFI System Table.
186 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
191 DxeTimerLibConstructor (
192 IN EFI_HANDLE ImageHandle
,
193 IN EFI_SYSTEM_TABLE
*SystemTable
196 EfiCreateProtocolNotifyEvent (
197 &gEfiTimerArchProtocolGuid
,
199 RegisterTimerArchProtocol
,