2 A non-functional instance of the Timer Library.
4 Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/BaseLib.h>
11 #include <Library/TimerLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/EmuThunkLib.h>
14 #include <Library/UefiBootServicesTableLib.h>
15 #include <Library/UefiLib.h>
17 #include <Protocol/Timer.h>
20 STATIC UINT64 gTimerPeriod
= 0;
21 STATIC EFI_TIMER_ARCH_PROTOCOL
*gTimerAp
= NULL
;
22 STATIC EFI_EVENT gTimerEvent
= NULL
;
23 STATIC VOID
*gRegistration
= NULL
;
27 RegisterTimerArchProtocol (
34 Status
= gBS
->LocateProtocol (&gEfiTimerArchProtocolGuid
, NULL
, (VOID
**)&gTimerAp
);
35 if (!EFI_ERROR (Status
)) {
36 Status
= gTimerAp
->GetTimerPeriod (gTimerAp
, &gTimerPeriod
);
37 ASSERT_EFI_ERROR (Status
);
39 // Convert to Nanoseconds.
40 gTimerPeriod
= MultU64x32 (gTimerPeriod
, 100);
42 if (gTimerEvent
== NULL
) {
43 Status
= gBS
->CreateEvent (EVT_TIMER
, 0, NULL
, NULL
, (VOID
**)&gTimerEvent
);
44 ASSERT_EFI_ERROR (Status
);
52 Stalls the CPU for at least the given number of microseconds.
54 Stalls the CPU for the number of microseconds specified by MicroSeconds.
56 @param MicroSeconds The minimum number of microseconds to delay.
58 @return The value of MicroSeconds inputted.
67 return NanoSecondDelay (MicroSeconds
* 1000);
72 Stalls the CPU for at least the given number of nanoseconds.
74 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
76 @param NanoSeconds The minimum number of nanoseconds to delay.
78 @return The value of NanoSeconds inputted.
88 UINT64 HundredNanoseconds
;
91 if ((gTimerPeriod
!= 0) &&
92 ((UINT64
)NanoSeconds
> gTimerPeriod
) &&
93 (EfiGetCurrentTpl () == TPL_APPLICATION
)) {
95 // This stall is long, so use gBS->WaitForEvent () to yield CPU to DXE Core
98 HundredNanoseconds
= DivU64x32 (NanoSeconds
, 100);
99 Status
= gBS
->SetTimer (gTimerEvent
, TimerRelative
, HundredNanoseconds
);
100 ASSERT_EFI_ERROR (Status
);
102 Status
= gBS
->WaitForEvent (sizeof (gTimerEvent
)/sizeof (EFI_EVENT
), &gTimerEvent
, &Index
);
103 ASSERT_EFI_ERROR (Status
);
106 gEmuThunk
->Sleep (NanoSeconds
);
113 Retrieves the current value of a 64-bit free running performance counter.
115 The counter can either count up by 1 or count down by 1. If the physical
116 performance counter counts by a larger increment, then the counter values
117 must be translated. The properties of the counter can be retrieved from
118 GetPerformanceCounterProperties().
120 @return The current value of the free running performance counter.
125 GetPerformanceCounter (
129 return gEmuThunk
->QueryPerformanceCounter ();
133 Retrieves the 64-bit frequency in Hz and the range of performance counter
136 If StartValue is not NULL, then the value that the performance counter starts
137 with immediately after is it rolls over is returned in StartValue. If
138 EndValue is not NULL, then the value that the performance counter end with
139 immediately before it rolls over is returned in EndValue. The 64-bit
140 frequency of the performance counter in Hz is always returned. If StartValue
141 is less than EndValue, then the performance counter counts up. If StartValue
142 is greater than EndValue, then the performance counter counts down. For
143 example, a 64-bit free running counter that counts up would have a StartValue
144 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
145 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
147 @param StartValue The value the performance counter starts with when it
149 @param EndValue The value that the performance counter ends with before
152 @return The frequency in Hz.
157 GetPerformanceCounterProperties (
158 OUT UINT64
*StartValue
, OPTIONAL
159 OUT UINT64
*EndValue OPTIONAL
163 if (StartValue
!= NULL
) {
166 if (EndValue
!= NULL
) {
167 *EndValue
= (UINT64
)-1LL;
170 return gEmuThunk
->QueryPerformanceFrequency ();
175 Register for the Timer AP protocol.
177 @param ImageHandle The firmware allocated handle for the EFI image.
178 @param SystemTable A pointer to the EFI System Table.
180 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
185 DxeTimerLibConstructor (
186 IN EFI_HANDLE ImageHandle
,
187 IN EFI_SYSTEM_TABLE
*SystemTable
190 EfiCreateProtocolNotifyEvent (
191 &gEfiTimerArchProtocolGuid
,
193 RegisterTimerArchProtocol
,
202 Converts elapsed ticks of performance counter to time in nanoseconds.
204 This function converts the elapsed ticks of running performance counter to
205 time value in unit of nanoseconds.
207 @param Ticks The number of elapsed ticks of running performance counter.
209 @return The elapsed time in nanoseconds.
214 GetTimeInNanoSecond (
223 Frequency
= GetPerformanceCounterProperties (NULL
, NULL
);
227 // Time = --------- x 1,000,000,000
230 NanoSeconds
= MultU64x32 (DivU64x64Remainder (Ticks
, Frequency
, &Remainder
), 1000000000u);
233 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
234 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
235 // i.e. highest bit set in Remainder should <= 33.
237 Shift
= MAX (0, HighBitSet64 (Remainder
) - 33);
238 Remainder
= RShiftU64 (Remainder
, (UINTN
) Shift
);
239 Frequency
= RShiftU64 (Frequency
, (UINTN
) Shift
);
240 NanoSeconds
+= DivU64x64Remainder (MultU64x32 (Remainder
, 1000000000u), Frequency
, NULL
);