]> git.proxmox.com Git - mirror_edk2.git/blame - EmulatorPkg/Library/PeiTimerLib/PeiTimerLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / EmulatorPkg / Library / PeiTimerLib / PeiTimerLib.c
CommitLineData
1ef41207 1/** @file\r
2 A non-functional instance of the Timer Library.\r
3\r
f6ce1a5c 4 Copyright (c) 2007 - 2023, 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
d652b458 10#include <Library/BaseLib.h>\r
1ef41207 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
a550d468 31 IN UINTN MicroSeconds\r
1ef41207 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
a550d468 50 IN UINTN NanoSeconds\r
1ef41207 51 )\r
52{\r
a550d468
MK
53 EMU_THUNK_PPI *ThunkPpi;\r
54 EFI_STATUS Status;\r
55 EMU_THUNK_PROTOCOL *Thunk;\r
1ef41207 56\r
57 //\r
d18d8a1d 58 // Locate EmuThunkPpi for\r
1ef41207 59 //\r
60 Status = PeiServicesLocatePpi (\r
a550d468
MK
61 &gEmuThunkPpiGuid,\r
62 0,\r
63 NULL,\r
64 (VOID **)&ThunkPpi\r
1ef41207 65 );\r
d18d8a1d 66 if (!EFI_ERROR (Status)) {\r
a550d468 67 Thunk = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();\r
f6ce1a5c 68 Thunk->Sleep (NanoSeconds);\r
1ef41207 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
a550d468
MK
92 EMU_THUNK_PPI *ThunkPpi;\r
93 EFI_STATUS Status;\r
94 EMU_THUNK_PROTOCOL *Thunk;\r
1ef41207 95\r
96 //\r
d18d8a1d 97 // Locate EmuThunkPpi for\r
1ef41207 98 //\r
99 Status = PeiServicesLocatePpi (\r
a550d468
MK
100 &gEmuThunkPpiGuid,\r
101 0,\r
102 NULL,\r
103 (VOID **)&ThunkPpi\r
1ef41207 104 );\r
d18d8a1d 105 if (!EFI_ERROR (Status)) {\r
a550d468
MK
106 Thunk = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();\r
107 return Thunk->QueryPerformanceCounter ();\r
1ef41207 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
a550d468
MK
139 OUT UINT64 *StartValue OPTIONAL,\r
140 OUT UINT64 *EndValue OPTIONAL\r
1ef41207 141 )\r
142{\r
a550d468
MK
143 EMU_THUNK_PPI *ThunkPpi;\r
144 EFI_STATUS Status;\r
145 EMU_THUNK_PROTOCOL *Thunk;\r
1ef41207 146\r
147 //\r
d18d8a1d 148 // Locate EmuThunkPpi for\r
1ef41207 149 //\r
150 Status = PeiServicesLocatePpi (\r
a550d468
MK
151 &gEmuThunkPpiGuid,\r
152 0,\r
153 NULL,\r
154 (VOID **)&ThunkPpi\r
1ef41207 155 );\r
156 if (!EFI_ERROR (Status)) {\r
157 if (StartValue != NULL) {\r
158 *StartValue = 0ULL;\r
159 }\r
a550d468 160\r
1ef41207 161 if (EndValue != NULL) {\r
162 *EndValue = (UINT64)-1LL;\r
163 }\r
d18d8a1d 164\r
a550d468
MK
165 Thunk = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();\r
166 return Thunk->QueryPerformanceFrequency ();\r
1ef41207 167 }\r
168\r
169 return 0;\r
170}\r
d652b458
MJ
171\r
172/**\r
173 Converts elapsed ticks of performance counter to time in nanoseconds.\r
174\r
175 This function converts the elapsed ticks of running performance counter to\r
176 time value in unit of nanoseconds.\r
177\r
178 @param Ticks The number of elapsed ticks of running performance counter.\r
179\r
180 @return The elapsed time in nanoseconds.\r
181\r
182**/\r
183UINT64\r
184EFIAPI\r
185GetTimeInNanoSecond (\r
186 IN UINT64 Ticks\r
187 )\r
188{\r
189 UINT64 Frequency;\r
190 UINT64 NanoSeconds;\r
191 UINT64 Remainder;\r
192 INTN Shift;\r
193\r
194 Frequency = GetPerformanceCounterProperties (NULL, NULL);\r
195\r
196 //\r
197 // Ticks\r
198 // Time = --------- x 1,000,000,000\r
199 // Frequency\r
200 //\r
201 NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);\r
202\r
203 //\r
204 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.\r
205 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,\r
206 // i.e. highest bit set in Remainder should <= 33.\r
207 //\r
a550d468
MK
208 Shift = MAX (0, HighBitSet64 (Remainder) - 33);\r
209 Remainder = RShiftU64 (Remainder, (UINTN)Shift);\r
210 Frequency = RShiftU64 (Frequency, (UINTN)Shift);\r
d652b458
MJ
211 NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);\r
212\r
213 return NanoSeconds;\r
214}\r