]> git.proxmox.com Git - mirror_edk2.git/blame - EmulatorPkg/Library/DxeTimerLib/DxeTimerLib.c
EmulatorPkg/DxeTimerLib: drop superfluous cast
[mirror_edk2.git] / EmulatorPkg / Library / DxeTimerLib / DxeTimerLib.c
CommitLineData
1ef41207 1/** @file\r
2 A non-functional instance of the Timer Library.\r
3\r
d652b458 4 Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>\r
e3ba31da 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
1ef41207 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/EmuThunkLib.h>\r
14#include <Library/UefiBootServicesTableLib.h>\r
15#include <Library/UefiLib.h>\r
16\r
17#include <Protocol/Timer.h>\r
18\r
19\r
20STATIC UINT64 gTimerPeriod = 0;\r
21STATIC EFI_TIMER_ARCH_PROTOCOL *gTimerAp = NULL;\r
22STATIC EFI_EVENT gTimerEvent = NULL;\r
23STATIC VOID *gRegistration = NULL;\r
24\r
25VOID\r
26EFIAPI\r
27RegisterTimerArchProtocol (\r
28 IN EFI_EVENT Event,\r
29 IN VOID *Context\r
30 )\r
31{\r
32 EFI_STATUS Status;\r
d18d8a1d 33\r
1ef41207 34 Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **)&gTimerAp);\r
d18d8a1d 35 if (!EFI_ERROR (Status)) {\r
1ef41207 36 Status = gTimerAp->GetTimerPeriod (gTimerAp, &gTimerPeriod);\r
37 ASSERT_EFI_ERROR (Status);\r
38\r
39 // Convert to Nanoseconds.\r
40 gTimerPeriod = MultU64x32 (gTimerPeriod, 100);\r
d18d8a1d 41\r
1ef41207 42 if (gTimerEvent == NULL) {\r
60d22274 43 Status = gBS->CreateEvent (EVT_TIMER, 0, NULL, NULL, &gTimerEvent);\r
1ef41207 44 ASSERT_EFI_ERROR (Status);\r
45 }\r
46 }\r
47}\r
48\r
49\r
50\r
51/**\r
52 Stalls the CPU for at least the given number of microseconds.\r
53\r
54 Stalls the CPU for the number of microseconds specified by MicroSeconds.\r
55\r
56 @param MicroSeconds The minimum number of microseconds to delay.\r
57\r
58 @return The value of MicroSeconds inputted.\r
59\r
60**/\r
61UINTN\r
62EFIAPI\r
63MicroSecondDelay (\r
64 IN UINTN MicroSeconds\r
65 )\r
66{\r
67 return NanoSecondDelay (MicroSeconds * 1000);\r
68}\r
69\r
70\r
71/**\r
72 Stalls the CPU for at least the given number of nanoseconds.\r
73\r
74 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r
75\r
76 @param NanoSeconds The minimum number of nanoseconds to delay.\r
77\r
78 @return The value of NanoSeconds inputted.\r
79\r
80**/\r
81UINTN\r
82EFIAPI\r
83NanoSecondDelay (\r
84 IN UINTN NanoSeconds\r
85 )\r
86{\r
87 EFI_STATUS Status;\r
88 UINT64 HundredNanoseconds;\r
89 UINTN Index;\r
d18d8a1d 90\r
91 if ((gTimerPeriod != 0) &&\r
92 ((UINT64)NanoSeconds > gTimerPeriod) &&\r
1ef41207 93 (EfiGetCurrentTpl () == TPL_APPLICATION)) {\r
94 //\r
95 // This stall is long, so use gBS->WaitForEvent () to yield CPU to DXE Core\r
96 //\r
d18d8a1d 97\r
1ef41207 98 HundredNanoseconds = DivU64x32 (NanoSeconds, 100);\r
99 Status = gBS->SetTimer (gTimerEvent, TimerRelative, HundredNanoseconds);\r
100 ASSERT_EFI_ERROR (Status);\r
101\r
102 Status = gBS->WaitForEvent (sizeof (gTimerEvent)/sizeof (EFI_EVENT), &gTimerEvent, &Index);\r
103 ASSERT_EFI_ERROR (Status);\r
d18d8a1d 104\r
1ef41207 105 } else {\r
106 gEmuThunk->Sleep (NanoSeconds);\r
107 }\r
108 return NanoSeconds;\r
109}\r
110\r
111\r
112/**\r
113 Retrieves the current value of a 64-bit free running performance counter.\r
114\r
115 The counter can either count up by 1 or count down by 1. If the physical\r
116 performance counter counts by a larger increment, then the counter values\r
117 must be translated. The properties of the counter can be retrieved from\r
118 GetPerformanceCounterProperties().\r
119\r
120 @return The current value of the free running performance counter.\r
121\r
122**/\r
123UINT64\r
124EFIAPI\r
125GetPerformanceCounter (\r
126 VOID\r
127 )\r
128{\r
129 return gEmuThunk->QueryPerformanceCounter ();\r
130}\r
131\r
132/**\r
133 Retrieves the 64-bit frequency in Hz and the range of performance counter\r
134 values.\r
135\r
136 If StartValue is not NULL, then the value that the performance counter starts\r
137 with immediately after is it rolls over is returned in StartValue. If\r
138 EndValue is not NULL, then the value that the performance counter end with\r
139 immediately before it rolls over is returned in EndValue. The 64-bit\r
140 frequency of the performance counter in Hz is always returned. If StartValue\r
141 is less than EndValue, then the performance counter counts up. If StartValue\r
142 is greater than EndValue, then the performance counter counts down. For\r
143 example, a 64-bit free running counter that counts up would have a StartValue\r
144 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r
145 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r
146\r
147 @param StartValue The value the performance counter starts with when it\r
148 rolls over.\r
149 @param EndValue The value that the performance counter ends with before\r
150 it rolls over.\r
151\r
152 @return The frequency in Hz.\r
153\r
154**/\r
155UINT64\r
156EFIAPI\r
157GetPerformanceCounterProperties (\r
158 OUT UINT64 *StartValue, OPTIONAL\r
159 OUT UINT64 *EndValue OPTIONAL\r
160 )\r
161{\r
162\r
163 if (StartValue != NULL) {\r
164 *StartValue = 0ULL;\r
165 }\r
166 if (EndValue != NULL) {\r
167 *EndValue = (UINT64)-1LL;\r
168 }\r
d18d8a1d 169\r
1ef41207 170 return gEmuThunk->QueryPerformanceFrequency ();\r
171}\r
172\r
173\r
174/**\r
175 Register for the Timer AP protocol.\r
176\r
177 @param ImageHandle The firmware allocated handle for the EFI image.\r
178 @param SystemTable A pointer to the EFI System Table.\r
179\r
180 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
181\r
182**/\r
183EFI_STATUS\r
184EFIAPI\r
185DxeTimerLibConstructor (\r
186 IN EFI_HANDLE ImageHandle,\r
187 IN EFI_SYSTEM_TABLE *SystemTable\r
188 )\r
189{\r
190 EfiCreateProtocolNotifyEvent (\r
191 &gEfiTimerArchProtocolGuid,\r
192 TPL_CALLBACK,\r
193 RegisterTimerArchProtocol,\r
194 NULL,\r
195 &gRegistration\r
196 );\r
197\r
198 return EFI_SUCCESS;\r
199}\r
200\r
d652b458
MJ
201/**\r
202 Converts elapsed ticks of performance counter to time in nanoseconds.\r
203\r
204 This function converts the elapsed ticks of running performance counter to\r
205 time value in unit of nanoseconds.\r
206\r
207 @param Ticks The number of elapsed ticks of running performance counter.\r
208\r
209 @return The elapsed time in nanoseconds.\r
210\r
211**/\r
212UINT64\r
213EFIAPI\r
214GetTimeInNanoSecond (\r
215 IN UINT64 Ticks\r
216 )\r
217{\r
218 UINT64 Frequency;\r
219 UINT64 NanoSeconds;\r
220 UINT64 Remainder;\r
221 INTN Shift;\r
222\r
223 Frequency = GetPerformanceCounterProperties (NULL, NULL);\r
224\r
225 //\r
226 // Ticks\r
227 // Time = --------- x 1,000,000,000\r
228 // Frequency\r
229 //\r
230 NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);\r
231\r
232 //\r
233 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.\r
234 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,\r
235 // i.e. highest bit set in Remainder should <= 33.\r
236 //\r
237 Shift = MAX (0, HighBitSet64 (Remainder) - 33);\r
238 Remainder = RShiftU64 (Remainder, (UINTN) Shift);\r
239 Frequency = RShiftU64 (Frequency, (UINTN) Shift);\r
240 NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);\r
241\r
242 return NanoSeconds;\r
243}\r