]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - EmulatorPkg/Library/PeiTimerLib/PeiTimerLib.c
EmulatorPkg/TimerLib: Add missing GetTimeInNanoSecond function
[mirror_edk2.git] / EmulatorPkg / Library / PeiTimerLib / PeiTimerLib.c
... / ...
CommitLineData
1/** @file\r
2 A non-functional instance of the Timer Library.\r
3\r
4 Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>\r
5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7**/\r
8\r
9#include <PiPei.h>\r
10#include <Library/BaseLib.h>\r
11#include <Library/TimerLib.h>\r
12#include <Library/DebugLib.h>\r
13#include <Library/PeiServicesLib.h>\r
14\r
15#include <Ppi/EmuThunk.h>\r
16#include <Protocol/EmuThunk.h>\r
17\r
18/**\r
19 Stalls the CPU for at least the given number of microseconds.\r
20\r
21 Stalls the CPU for the number of microseconds specified by MicroSeconds.\r
22\r
23 @param MicroSeconds The minimum number of microseconds to delay.\r
24\r
25 @return The value of MicroSeconds inputted.\r
26\r
27**/\r
28UINTN\r
29EFIAPI\r
30MicroSecondDelay (\r
31 IN UINTN MicroSeconds\r
32 )\r
33{\r
34 return NanoSecondDelay (MicroSeconds * 1000);\r
35}\r
36\r
37/**\r
38 Stalls the CPU for at least the given number of nanoseconds.\r
39\r
40 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r
41\r
42 @param NanoSeconds The minimum number of nanoseconds to delay.\r
43\r
44 @return The value of NanoSeconds inputted.\r
45\r
46**/\r
47UINTN\r
48EFIAPI\r
49NanoSecondDelay (\r
50 IN UINTN NanoSeconds\r
51 )\r
52{\r
53 EMU_THUNK_PPI *ThunkPpi;\r
54 EFI_STATUS Status;\r
55 EMU_THUNK_PROTOCOL *Thunk;\r
56\r
57 //\r
58 // Locate EmuThunkPpi for\r
59 //\r
60 Status = PeiServicesLocatePpi (\r
61 &gEmuThunkPpiGuid,\r
62 0,\r
63 NULL,\r
64 (VOID **) &ThunkPpi\r
65 );\r
66 if (!EFI_ERROR (Status)) {\r
67 Thunk = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();\r
68 Thunk->Sleep (NanoSeconds * 100);\r
69 return NanoSeconds;\r
70 }\r
71\r
72 return 0;\r
73}\r
74\r
75/**\r
76 Retrieves the current value of a 64-bit free running performance counter.\r
77\r
78 The counter can either count up by 1 or count down by 1. If the physical\r
79 performance counter counts by a larger increment, then the counter values\r
80 must be translated. The properties of the counter can be retrieved from\r
81 GetPerformanceCounterProperties().\r
82\r
83 @return The current value of the free running performance counter.\r
84\r
85**/\r
86UINT64\r
87EFIAPI\r
88GetPerformanceCounter (\r
89 VOID\r
90 )\r
91{\r
92 EMU_THUNK_PPI *ThunkPpi;\r
93 EFI_STATUS Status;\r
94 EMU_THUNK_PROTOCOL *Thunk;\r
95\r
96 //\r
97 // Locate EmuThunkPpi for\r
98 //\r
99 Status = PeiServicesLocatePpi (\r
100 &gEmuThunkPpiGuid,\r
101 0,\r
102 NULL,\r
103 (VOID **) &ThunkPpi\r
104 );\r
105 if (!EFI_ERROR (Status)) {\r
106 Thunk = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();\r
107 return Thunk->QueryPerformanceCounter ();\r
108 }\r
109\r
110 return 0;\r
111}\r
112\r
113/**\r
114 Retrieves the 64-bit frequency in Hz and the range of performance counter\r
115 values.\r
116\r
117 If StartValue is not NULL, then the value that the performance counter starts\r
118 with immediately after is it rolls over is returned in StartValue. If\r
119 EndValue is not NULL, then the value that the performance counter end with\r
120 immediately before it rolls over is returned in EndValue. The 64-bit\r
121 frequency of the performance counter in Hz is always returned. If StartValue\r
122 is less than EndValue, then the performance counter counts up. If StartValue\r
123 is greater than EndValue, then the performance counter counts down. For\r
124 example, a 64-bit free running counter that counts up would have a StartValue\r
125 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r
126 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r
127\r
128 @param StartValue The value the performance counter starts with when it\r
129 rolls over.\r
130 @param EndValue The value that the performance counter ends with before\r
131 it rolls over.\r
132\r
133 @return The frequency in Hz.\r
134\r
135**/\r
136UINT64\r
137EFIAPI\r
138GetPerformanceCounterProperties (\r
139 OUT UINT64 *StartValue, OPTIONAL\r
140 OUT UINT64 *EndValue OPTIONAL\r
141 )\r
142{\r
143 EMU_THUNK_PPI *ThunkPpi;\r
144 EFI_STATUS Status;\r
145 EMU_THUNK_PROTOCOL *Thunk;\r
146\r
147 //\r
148 // Locate EmuThunkPpi for\r
149 //\r
150 Status = PeiServicesLocatePpi (\r
151 &gEmuThunkPpiGuid,\r
152 0,\r
153 NULL,\r
154 (VOID **) &ThunkPpi\r
155 );\r
156 if (!EFI_ERROR (Status)) {\r
157 if (StartValue != NULL) {\r
158 *StartValue = 0ULL;\r
159 }\r
160 if (EndValue != NULL) {\r
161 *EndValue = (UINT64)-1LL;\r
162 }\r
163\r
164 Thunk = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();\r
165 return Thunk->QueryPerformanceFrequency ();\r
166 }\r
167\r
168 return 0;\r
169}\r
170\r
171/**\r
172 Converts elapsed ticks of performance counter to time in nanoseconds.\r
173\r
174 This function converts the elapsed ticks of running performance counter to\r
175 time value in unit of nanoseconds.\r
176\r
177 @param Ticks The number of elapsed ticks of running performance counter.\r
178\r
179 @return The elapsed time in nanoseconds.\r
180\r
181**/\r
182UINT64\r
183EFIAPI\r
184GetTimeInNanoSecond (\r
185 IN UINT64 Ticks\r
186 )\r
187{\r
188 UINT64 Frequency;\r
189 UINT64 NanoSeconds;\r
190 UINT64 Remainder;\r
191 INTN Shift;\r
192\r
193 Frequency = GetPerformanceCounterProperties (NULL, NULL);\r
194\r
195 //\r
196 // Ticks\r
197 // Time = --------- x 1,000,000,000\r
198 // Frequency\r
199 //\r
200 NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);\r
201\r
202 //\r
203 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.\r
204 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,\r
205 // i.e. highest bit set in Remainder should <= 33.\r
206 //\r
207 Shift = MAX (0, HighBitSet64 (Remainder) - 33);\r
208 Remainder = RShiftU64 (Remainder, (UINTN) Shift);\r
209 Frequency = RShiftU64 (Frequency, (UINTN) Shift);\r
210 NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);\r
211\r
212 return NanoSeconds;\r
213}\r