]>
git.proxmox.com Git - mirror_edk2.git/blob - EmulatorPkg/Library/DxeCoreTimerLib/DxeCoreTimerLib.c
513a5aaa7c3eb2958646826d29de9770fe30ed35
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>
22 Stalls the CPU for at least the given number of microseconds.
24 Stalls the CPU for the number of microseconds specified by MicroSeconds.
26 @param MicroSeconds The minimum number of microseconds to delay.
28 @return The value of MicroSeconds inputted.
37 return NanoSecondDelay (MicroSeconds
* 1000);
42 Stalls the CPU for at least the given number of nanoseconds.
44 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
46 @param NanoSeconds The minimum number of nanoseconds to delay.
48 @return The value of NanoSeconds inputted.
57 gEmuThunk
->Sleep (NanoSeconds
);
63 Retrieves the current value of a 64-bit free running performance counter.
65 The counter can either count up by 1 or count down by 1. If the physical
66 performance counter counts by a larger increment, then the counter values
67 must be translated. The properties of the counter can be retrieved from
68 GetPerformanceCounterProperties().
70 @return The current value of the free running performance counter.
75 GetPerformanceCounter (
79 return gEmuThunk
->QueryPerformanceCounter ();
83 Retrieves the 64-bit frequency in Hz and the range of performance counter
86 If StartValue is not NULL, then the value that the performance counter starts
87 with immediately after is it rolls over is returned in StartValue. If
88 EndValue is not NULL, then the value that the performance counter end with
89 immediately before it rolls over is returned in EndValue. The 64-bit
90 frequency of the performance counter in Hz is always returned. If StartValue
91 is less than EndValue, then the performance counter counts up. If StartValue
92 is greater than EndValue, then the performance counter counts down. For
93 example, a 64-bit free running counter that counts up would have a StartValue
94 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
95 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
97 @param StartValue The value the performance counter starts with when it
99 @param EndValue The value that the performance counter ends with before
102 @return The frequency in Hz.
107 GetPerformanceCounterProperties (
108 OUT UINT64
*StartValue OPTIONAL
,
109 OUT UINT64
*EndValue OPTIONAL
113 if (StartValue
!= NULL
) {
116 if (EndValue
!= NULL
) {
117 *EndValue
= (UINT64
)-1LL;
120 return gEmuThunk
->QueryPerformanceFrequency ();
124 Converts elapsed ticks of performance counter to time in nanoseconds.
126 This function converts the elapsed ticks of running performance counter to
127 time value in unit of nanoseconds.
129 @param Ticks The number of elapsed ticks of running performance counter.
131 @return The elapsed time in nanoseconds.
136 GetTimeInNanoSecond (
145 Frequency
= GetPerformanceCounterProperties (NULL
, NULL
);
149 // Time = --------- x 1,000,000,000
152 NanoSeconds
= MultU64x32 (DivU64x64Remainder (Ticks
, Frequency
, &Remainder
), 1000000000u);
155 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
156 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
157 // i.e. highest bit set in Remainder should <= 33.
159 Shift
= MAX (0, HighBitSet64 (Remainder
) - 33);
160 Remainder
= RShiftU64 (Remainder
, (UINTN
) Shift
);
161 Frequency
= RShiftU64 (Frequency
, (UINTN
) Shift
);
162 NanoSeconds
+= DivU64x64Remainder (MultU64x32 (Remainder
, 1000000000u), Frequency
, NULL
);