]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/CpuTimerLib/CpuTimerLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / UefiCpuPkg / Library / CpuTimerLib / CpuTimerLib.c
1 /** @file
2 CPUID Leaf 0x15 for Core Crystal Clock frequency instance of Timer Library.
3
4 Copyright (c) 2019 Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Base.h>
10 #include <Library/TimerLib.h>
11 #include <Library/BaseLib.h>
12 #include <Library/PcdLib.h>
13 #include <Library/DebugLib.h>
14 #include <Register/Cpuid.h>
15
16 GUID mCpuCrystalFrequencyHobGuid = {
17 0xe1ec5ad0, 0x8569, 0x46bd, { 0x8d, 0xcd, 0x3b, 0x9f, 0x6f, 0x45, 0x82, 0x7a }
18 };
19
20 /**
21 Internal function to retrieves the 64-bit frequency in Hz.
22
23 Internal function to retrieves the 64-bit frequency in Hz.
24
25 @return The frequency in Hz.
26
27 **/
28 UINT64
29 InternalGetPerformanceCounterFrequency (
30 VOID
31 );
32
33 /**
34 CPUID Leaf 0x15 for Core Crystal Clock Frequency.
35
36 The TSC counting frequency is determined by using CPUID leaf 0x15. Frequency in MHz = Core XTAL frequency * EBX/EAX.
37 In newer flavors of the CPU, core xtal frequency is returned in ECX or 0 if not supported.
38 @return The number of TSC counts per second.
39
40 **/
41 UINT64
42 CpuidCoreClockCalculateTscFrequency (
43 VOID
44 )
45 {
46 UINT64 TscFrequency;
47 UINT64 CoreXtalFrequency;
48 UINT32 RegEax;
49 UINT32 RegEbx;
50 UINT32 RegEcx;
51
52 //
53 // Use CPUID leaf 0x15 Time Stamp Counter and Nominal Core Crystal Clock Information
54 // EBX returns 0 if not supported. ECX, if non zero, provides Core Xtal Frequency in hertz.
55 // TSC frequency = (ECX, Core Xtal Frequency) * EBX/EAX.
56 //
57 AsmCpuid (CPUID_TIME_STAMP_COUNTER, &RegEax, &RegEbx, &RegEcx, NULL);
58
59 //
60 // If EAX or EBX returns 0, the XTAL ratio is not enumerated.
61 //
62 if ((RegEax == 0) || (RegEbx == 0)) {
63 ASSERT (RegEax != 0);
64 ASSERT (RegEbx != 0);
65 return 0;
66 }
67
68 //
69 // If ECX returns 0, the XTAL frequency is not enumerated.
70 // And PcdCpuCoreCrystalClockFrequency defined should base on processor series.
71 //
72 if (RegEcx == 0) {
73 CoreXtalFrequency = PcdGet64 (PcdCpuCoreCrystalClockFrequency);
74 } else {
75 CoreXtalFrequency = (UINT64)RegEcx;
76 }
77
78 //
79 // Calculate TSC frequency = (ECX, Core Xtal Frequency) * EBX/EAX
80 //
81 TscFrequency = DivU64x32 (MultU64x32 (CoreXtalFrequency, RegEbx) + (UINT64)(RegEax >> 1), RegEax);
82
83 return TscFrequency;
84 }
85
86 /**
87 Stalls the CPU for at least the given number of ticks.
88
89 Stalls the CPU for at least the given number of ticks. It's invoked by
90 MicroSecondDelay() and NanoSecondDelay().
91
92 @param Delay A period of time to delay in ticks.
93
94 **/
95 VOID
96 InternalCpuDelay (
97 IN UINT64 Delay
98 )
99 {
100 UINT64 Ticks;
101
102 //
103 // The target timer count is calculated here
104 //
105 Ticks = AsmReadTsc () + Delay;
106
107 //
108 // Wait until time out
109 // Timer wrap-arounds are NOT handled correctly by this function.
110 // Thus, this function must be called within 10 years of reset since
111 // Intel guarantees a minimum of 10 years before the TSC wraps.
112 //
113 while (AsmReadTsc () <= Ticks) {
114 CpuPause ();
115 }
116 }
117
118 /**
119 Stalls the CPU for at least the given number of microseconds.
120
121 Stalls the CPU for the number of microseconds specified by MicroSeconds.
122
123 @param[in] MicroSeconds The minimum number of microseconds to delay.
124
125 @return MicroSeconds
126
127 **/
128 UINTN
129 EFIAPI
130 MicroSecondDelay (
131 IN UINTN MicroSeconds
132 )
133 {
134 InternalCpuDelay (
135 DivU64x32 (
136 MultU64x64 (
137 MicroSeconds,
138 InternalGetPerformanceCounterFrequency ()
139 ),
140 1000000u
141 )
142 );
143
144 return MicroSeconds;
145 }
146
147 /**
148 Stalls the CPU for at least the given number of nanoseconds.
149
150 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
151
152 @param NanoSeconds The minimum number of nanoseconds to delay.
153
154 @return NanoSeconds
155
156 **/
157 UINTN
158 EFIAPI
159 NanoSecondDelay (
160 IN UINTN NanoSeconds
161 )
162 {
163 InternalCpuDelay (
164 DivU64x32 (
165 MultU64x64 (
166 NanoSeconds,
167 InternalGetPerformanceCounterFrequency ()
168 ),
169 1000000000u
170 )
171 );
172
173 return NanoSeconds;
174 }
175
176 /**
177 Retrieves the current value of a 64-bit free running performance counter.
178
179 Retrieves the current value of a 64-bit free running performance counter. The
180 counter can either count up by 1 or count down by 1. If the physical
181 performance counter counts by a larger increment, then the counter values
182 must be translated. The properties of the counter can be retrieved from
183 GetPerformanceCounterProperties().
184
185 @return The current value of the free running performance counter.
186
187 **/
188 UINT64
189 EFIAPI
190 GetPerformanceCounter (
191 VOID
192 )
193 {
194 return AsmReadTsc ();
195 }
196
197 /**
198 Retrieves the 64-bit frequency in Hz and the range of performance counter
199 values.
200
201 If StartValue is not NULL, then the value that the performance counter starts
202 with immediately after is it rolls over is returned in StartValue. If
203 EndValue is not NULL, then the value that the performance counter end with
204 immediately before it rolls over is returned in EndValue. The 64-bit
205 frequency of the performance counter in Hz is always returned. If StartValue
206 is less than EndValue, then the performance counter counts up. If StartValue
207 is greater than EndValue, then the performance counter counts down. For
208 example, a 64-bit free running counter that counts up would have a StartValue
209 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
210 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
211
212 @param StartValue The value the performance counter starts with when it
213 rolls over.
214 @param EndValue The value that the performance counter ends with before
215 it rolls over.
216
217 @return The frequency in Hz.
218
219 **/
220 UINT64
221 EFIAPI
222 GetPerformanceCounterProperties (
223 OUT UINT64 *StartValue OPTIONAL,
224 OUT UINT64 *EndValue OPTIONAL
225 )
226 {
227 if (StartValue != NULL) {
228 *StartValue = 0;
229 }
230
231 if (EndValue != NULL) {
232 *EndValue = 0xffffffffffffffffULL;
233 }
234
235 return InternalGetPerformanceCounterFrequency ();
236 }
237
238 /**
239 Converts elapsed ticks of performance counter to time in nanoseconds.
240
241 This function converts the elapsed ticks of running performance counter to
242 time value in unit of nanoseconds.
243
244 @param Ticks The number of elapsed ticks of running performance counter.
245
246 @return The elapsed time in nanoseconds.
247
248 **/
249 UINT64
250 EFIAPI
251 GetTimeInNanoSecond (
252 IN UINT64 Ticks
253 )
254 {
255 UINT64 Frequency;
256 UINT64 NanoSeconds;
257 UINT64 Remainder;
258 INTN Shift;
259
260 Frequency = GetPerformanceCounterProperties (NULL, NULL);
261
262 //
263 // Ticks
264 // Time = --------- x 1,000,000,000
265 // Frequency
266 //
267 NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);
268
269 //
270 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
271 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
272 // i.e. highest bit set in Remainder should <= 33.
273 //
274 Shift = MAX (0, HighBitSet64 (Remainder) - 33);
275 Remainder = RShiftU64 (Remainder, (UINTN)Shift);
276 Frequency = RShiftU64 (Frequency, (UINTN)Shift);
277 NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);
278
279 return NanoSeconds;
280 }