TianoCore EDK2 master
Loading...
Searching...
No Matches
PeiTimerLib.c
Go to the documentation of this file.
1
9#include <PiPei.h>
10#include <Library/BaseLib.h>
11#include <Library/TimerLib.h>
12#include <Library/DebugLib.h>
14
15#include <Ppi/EmuThunk.h>
16#include <Protocol/EmuThunk.h>
17
29EFIAPI
31 IN UINTN MicroSeconds
32 )
33{
34 return NanoSecondDelay (MicroSeconds * 1000);
35}
36
48EFIAPI
50 IN UINTN NanoSeconds
51 )
52{
53 EMU_THUNK_PPI *ThunkPpi;
54 EFI_STATUS Status;
55 EMU_THUNK_PROTOCOL *Thunk;
56
57 //
58 // Locate EmuThunkPpi for
59 //
60 Status = PeiServicesLocatePpi (
61 &gEmuThunkPpiGuid,
62 0,
63 NULL,
64 (VOID **)&ThunkPpi
65 );
66 if (!EFI_ERROR (Status)) {
67 Thunk = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();
68 Thunk->Sleep (NanoSeconds);
69 return NanoSeconds;
70 }
71
72 return 0;
73}
74
86UINT64
87EFIAPI
89 VOID
90 )
91{
92 EMU_THUNK_PPI *ThunkPpi;
93 EFI_STATUS Status;
94 EMU_THUNK_PROTOCOL *Thunk;
95
96 //
97 // Locate EmuThunkPpi for
98 //
99 Status = PeiServicesLocatePpi (
100 &gEmuThunkPpiGuid,
101 0,
102 NULL,
103 (VOID **)&ThunkPpi
104 );
105 if (!EFI_ERROR (Status)) {
106 Thunk = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();
107 return Thunk->QueryPerformanceCounter ();
108 }
109
110 return 0;
111}
112
136UINT64
137EFIAPI
139 OUT UINT64 *StartValue OPTIONAL,
140 OUT UINT64 *EndValue OPTIONAL
141 )
142{
143 EMU_THUNK_PPI *ThunkPpi;
144 EFI_STATUS Status;
145 EMU_THUNK_PROTOCOL *Thunk;
146
147 //
148 // Locate EmuThunkPpi for
149 //
150 Status = PeiServicesLocatePpi (
151 &gEmuThunkPpiGuid,
152 0,
153 NULL,
154 (VOID **)&ThunkPpi
155 );
156 if (!EFI_ERROR (Status)) {
157 if (StartValue != NULL) {
158 *StartValue = 0ULL;
159 }
160
161 if (EndValue != NULL) {
162 *EndValue = (UINT64)-1LL;
163 }
164
165 Thunk = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();
166 return Thunk->QueryPerformanceFrequency ();
167 }
168
169 return 0;
170}
171
183UINT64
184EFIAPI
186 IN UINT64 Ticks
187 )
188{
189 UINT64 Frequency;
190 UINT64 NanoSeconds;
191 UINT64 Remainder;
192 INTN Shift;
193
195
196 //
197 // Ticks
198 // Time = --------- x 1,000,000,000
199 // Frequency
200 //
201 NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);
202
203 //
204 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
205 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
206 // i.e. highest bit set in Remainder should <= 33.
207 //
208 Shift = MAX (0, HighBitSet64 (Remainder) - 33);
209 Remainder = RShiftU64 (Remainder, (UINTN)Shift);
210 Frequency = RShiftU64 (Frequency, (UINTN)Shift);
211 NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);
212
213 return NanoSeconds;
214}
UINT64 UINTN
INT64 INTN
UINT64 EFIAPI RShiftU64(IN UINT64 Operand, IN UINTN Count)
Definition: RShiftU64.c:28
UINT64 EFIAPI MultU64x32(IN UINT64 Multiplicand, IN UINT32 Multiplier)
Definition: MultU64x32.c:27
UINT64 EFIAPI DivU64x64Remainder(IN UINT64 Dividend, IN UINT64 Divisor, OUT UINT64 *Remainder OPTIONAL)
INTN EFIAPI HighBitSet64(IN UINT64 Operand)
Definition: HighBitSet64.c:27
EFI_STATUS EFIAPI PeiServicesLocatePpi(IN CONST EFI_GUID *Guid, IN UINTN Instance, IN OUT EFI_PEI_PPI_DESCRIPTOR **PpiDescriptor, IN OUT VOID **Ppi)
#define NULL
Definition: Base.h:319
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define MAX(a, b)
Definition: Base.h:992
UINT64 EFIAPI GetPerformanceCounterProperties(OUT UINT64 *StartValue OPTIONAL, OUT UINT64 *EndValue OPTIONAL)
Definition: PeiTimerLib.c:138
UINT64 EFIAPI GetTimeInNanoSecond(IN UINT64 Ticks)
Definition: PeiTimerLib.c:185
UINT64 EFIAPI GetPerformanceCounter(VOID)
Definition: PeiTimerLib.c:88
UINTN EFIAPI MicroSecondDelay(IN UINTN MicroSeconds)
Definition: PeiTimerLib.c:30
UINTN EFIAPI NanoSecondDelay(IN UINTN NanoSeconds)
Definition: PeiTimerLib.c:49
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29