]> git.proxmox.com Git - mirror_edk2.git/blob - EmulatorPkg/Library/PeiTimerLib/PeiTimerLib.c
EmulatorPkg/TimerLib: Add missing GetTimeInNanoSecond function
[mirror_edk2.git] / EmulatorPkg / Library / PeiTimerLib / PeiTimerLib.c
1 /** @file
2 A non-functional instance of the Timer Library.
3
4 Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <PiPei.h>
10 #include <Library/BaseLib.h>
11 #include <Library/TimerLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/PeiServicesLib.h>
14
15 #include <Ppi/EmuThunk.h>
16 #include <Protocol/EmuThunk.h>
17
18 /**
19 Stalls the CPU for at least the given number of microseconds.
20
21 Stalls the CPU for the number of microseconds specified by MicroSeconds.
22
23 @param MicroSeconds The minimum number of microseconds to delay.
24
25 @return The value of MicroSeconds inputted.
26
27 **/
28 UINTN
29 EFIAPI
30 MicroSecondDelay (
31 IN UINTN MicroSeconds
32 )
33 {
34 return NanoSecondDelay (MicroSeconds * 1000);
35 }
36
37 /**
38 Stalls the CPU for at least the given number of nanoseconds.
39
40 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
41
42 @param NanoSeconds The minimum number of nanoseconds to delay.
43
44 @return The value of NanoSeconds inputted.
45
46 **/
47 UINTN
48 EFIAPI
49 NanoSecondDelay (
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 * 100);
69 return NanoSeconds;
70 }
71
72 return 0;
73 }
74
75 /**
76 Retrieves the current value of a 64-bit free running performance counter.
77
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().
82
83 @return The current value of the free running performance counter.
84
85 **/
86 UINT64
87 EFIAPI
88 GetPerformanceCounter (
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
113 /**
114 Retrieves the 64-bit frequency in Hz and the range of performance counter
115 values.
116
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.
127
128 @param StartValue The value the performance counter starts with when it
129 rolls over.
130 @param EndValue The value that the performance counter ends with before
131 it rolls over.
132
133 @return The frequency in Hz.
134
135 **/
136 UINT64
137 EFIAPI
138 GetPerformanceCounterProperties (
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 if (EndValue != NULL) {
161 *EndValue = (UINT64)-1LL;
162 }
163
164 Thunk = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();
165 return Thunk->QueryPerformanceFrequency ();
166 }
167
168 return 0;
169 }
170
171 /**
172 Converts elapsed ticks of performance counter to time in nanoseconds.
173
174 This function converts the elapsed ticks of running performance counter to
175 time value in unit of nanoseconds.
176
177 @param Ticks The number of elapsed ticks of running performance counter.
178
179 @return The elapsed time in nanoseconds.
180
181 **/
182 UINT64
183 EFIAPI
184 GetTimeInNanoSecond (
185 IN UINT64 Ticks
186 )
187 {
188 UINT64 Frequency;
189 UINT64 NanoSeconds;
190 UINT64 Remainder;
191 INTN Shift;
192
193 Frequency = GetPerformanceCounterProperties (NULL, NULL);
194
195 //
196 // Ticks
197 // Time = --------- x 1,000,000,000
198 // Frequency
199 //
200 NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);
201
202 //
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.
206 //
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);
211
212 return NanoSeconds;
213 }