]>
git.proxmox.com Git - mirror_edk2.git/blob - EmulatorPkg/Library/PeiTimerLib/PeiTimerLib.c
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/PeiServicesLib.h>
15 #include <Ppi/EmuThunk.h>
16 #include <Protocol/EmuThunk.h>
19 Stalls the CPU for at least the given number of microseconds.
21 Stalls the CPU for the number of microseconds specified by MicroSeconds.
23 @param MicroSeconds The minimum number of microseconds to delay.
25 @return The value of MicroSeconds inputted.
34 return NanoSecondDelay (MicroSeconds
* 1000);
38 Stalls the CPU for at least the given number of nanoseconds.
40 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
42 @param NanoSeconds The minimum number of nanoseconds to delay.
44 @return The value of NanoSeconds inputted.
53 EMU_THUNK_PPI
*ThunkPpi
;
55 EMU_THUNK_PROTOCOL
*Thunk
;
58 // Locate EmuThunkPpi for
60 Status
= PeiServicesLocatePpi (
66 if (!EFI_ERROR (Status
)) {
67 Thunk
= (EMU_THUNK_PROTOCOL
*)ThunkPpi
->Thunk ();
68 Thunk
->Sleep (NanoSeconds
* 100);
76 Retrieves the current value of a 64-bit free running performance counter.
78 The counter can either count up by 1 or count down by 1. If the physical
79 performance counter counts by a larger increment, then the counter values
80 must be translated. The properties of the counter can be retrieved from
81 GetPerformanceCounterProperties().
83 @return The current value of the free running performance counter.
88 GetPerformanceCounter (
92 EMU_THUNK_PPI
*ThunkPpi
;
94 EMU_THUNK_PROTOCOL
*Thunk
;
97 // Locate EmuThunkPpi for
99 Status
= PeiServicesLocatePpi (
105 if (!EFI_ERROR (Status
)) {
106 Thunk
= (EMU_THUNK_PROTOCOL
*)ThunkPpi
->Thunk ();
107 return Thunk
->QueryPerformanceCounter ();
114 Retrieves the 64-bit frequency in Hz and the range of performance counter
117 If StartValue is not NULL, then the value that the performance counter starts
118 with immediately after is it rolls over is returned in StartValue. If
119 EndValue is not NULL, then the value that the performance counter end with
120 immediately before it rolls over is returned in EndValue. The 64-bit
121 frequency of the performance counter in Hz is always returned. If StartValue
122 is less than EndValue, then the performance counter counts up. If StartValue
123 is greater than EndValue, then the performance counter counts down. For
124 example, a 64-bit free running counter that counts up would have a StartValue
125 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
126 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
128 @param StartValue The value the performance counter starts with when it
130 @param EndValue The value that the performance counter ends with before
133 @return The frequency in Hz.
138 GetPerformanceCounterProperties (
139 OUT UINT64
*StartValue
, OPTIONAL
140 OUT UINT64
*EndValue OPTIONAL
143 EMU_THUNK_PPI
*ThunkPpi
;
145 EMU_THUNK_PROTOCOL
*Thunk
;
148 // Locate EmuThunkPpi for
150 Status
= PeiServicesLocatePpi (
156 if (!EFI_ERROR (Status
)) {
157 if (StartValue
!= NULL
) {
160 if (EndValue
!= NULL
) {
161 *EndValue
= (UINT64
)-1LL;
164 Thunk
= (EMU_THUNK_PROTOCOL
*)ThunkPpi
->Thunk ();
165 return Thunk
->QueryPerformanceFrequency ();
172 Converts elapsed ticks of performance counter to time in nanoseconds.
174 This function converts the elapsed ticks of running performance counter to
175 time value in unit of nanoseconds.
177 @param Ticks The number of elapsed ticks of running performance counter.
179 @return The elapsed time in nanoseconds.
184 GetTimeInNanoSecond (
193 Frequency
= GetPerformanceCounterProperties (NULL
, NULL
);
197 // Time = --------- x 1,000,000,000
200 NanoSeconds
= MultU64x32 (DivU64x64Remainder (Ticks
, Frequency
, &Remainder
), 1000000000u);
203 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
204 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
205 // i.e. highest bit set in Remainder should <= 33.
207 Shift
= MAX (0, HighBitSet64 (Remainder
) - 33);
208 Remainder
= RShiftU64 (Remainder
, (UINTN
) Shift
);
209 Frequency
= RShiftU64 (Frequency
, (UINTN
) Shift
);
210 NanoSeconds
+= DivU64x64Remainder (MultU64x32 (Remainder
, 1000000000u), Frequency
, NULL
);