2 CPUID Leaf 0x15 for Core Crystal Clock frequency instance of Timer Library.
4 Copyright (c) 2019 Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
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>
16 GUID mCpuCrystalFrequencyHobGuid
= { 0xe1ec5ad0, 0x8569, 0x46bd, { 0x8d, 0xcd, 0x3b, 0x9f, 0x6f, 0x45, 0x82, 0x7a } };
19 Internal function to retrieves the 64-bit frequency in Hz.
21 Internal function to retrieves the 64-bit frequency in Hz.
23 @return The frequency in Hz.
27 InternalGetPerformanceCounterFrequency (
32 CPUID Leaf 0x15 for Core Crystal Clock Frequency.
34 The TSC counting frequency is determined by using CPUID leaf 0x15. Frequency in MHz = Core XTAL frequency * EBX/EAX.
35 In newer flavors of the CPU, core xtal frequency is returned in ECX or 0 if not supported.
36 @return The number of TSC counts per second.
40 CpuidCoreClockCalculateTscFrequency (
45 UINT64 CoreXtalFrequency
;
51 // Use CPUID leaf 0x15 Time Stamp Counter and Nominal Core Crystal Clock Information
52 // EBX returns 0 if not supported. ECX, if non zero, provides Core Xtal Frequency in hertz.
53 // TSC frequency = (ECX, Core Xtal Frequency) * EBX/EAX.
55 AsmCpuid (CPUID_TIME_STAMP_COUNTER
, &RegEax
, &RegEbx
, &RegEcx
, NULL
);
58 // If EAX or EBX returns 0, the XTAL ratio is not enumerated.
60 if (RegEax
== 0 || RegEbx
==0 ) {
66 // If ECX returns 0, the XTAL frequency is not enumerated.
67 // And PcdCpuCoreCrystalClockFrequency defined should base on processor series.
70 CoreXtalFrequency
= PcdGet64 (PcdCpuCoreCrystalClockFrequency
);
72 CoreXtalFrequency
= (UINT64
) RegEcx
;
76 // Calculate TSC frequency = (ECX, Core Xtal Frequency) * EBX/EAX
78 TscFrequency
= DivU64x32 (MultU64x32 (CoreXtalFrequency
, RegEbx
) + (UINT64
)(RegEax
>> 1), RegEax
);
84 Stalls the CPU for at least the given number of ticks.
86 Stalls the CPU for at least the given number of ticks. It's invoked by
87 MicroSecondDelay() and NanoSecondDelay().
89 @param Delay A period of time to delay in ticks.
100 // The target timer count is calculated here
102 Ticks
= AsmReadTsc() + Delay
;
105 // Wait until time out
106 // Timer wrap-arounds are NOT handled correctly by this function.
107 // Thus, this function must be called within 10 years of reset since
108 // Intel guarantees a minimum of 10 years before the TSC wraps.
110 while (AsmReadTsc() <= Ticks
) {
116 Stalls the CPU for at least the given number of microseconds.
118 Stalls the CPU for the number of microseconds specified by MicroSeconds.
120 @param[in] MicroSeconds The minimum number of microseconds to delay.
128 IN UINTN MicroSeconds
136 InternalGetPerformanceCounterFrequency ()
146 Stalls the CPU for at least the given number of nanoseconds.
148 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
150 @param NanoSeconds The minimum number of nanoseconds to delay.
166 InternalGetPerformanceCounterFrequency ()
176 Retrieves the current value of a 64-bit free running performance counter.
178 Retrieves the current value of a 64-bit free running performance counter. The
179 counter can either count up by 1 or count down by 1. If the physical
180 performance counter counts by a larger increment, then the counter values
181 must be translated. The properties of the counter can be retrieved from
182 GetPerformanceCounterProperties().
184 @return The current value of the free running performance counter.
189 GetPerformanceCounter (
193 return AsmReadTsc ();
197 Retrieves the 64-bit frequency in Hz and the range of performance counter
200 If StartValue is not NULL, then the value that the performance counter starts
201 with immediately after is it rolls over is returned in StartValue. If
202 EndValue is not NULL, then the value that the performance counter end with
203 immediately before it rolls over is returned in EndValue. The 64-bit
204 frequency of the performance counter in Hz is always returned. If StartValue
205 is less than EndValue, then the performance counter counts up. If StartValue
206 is greater than EndValue, then the performance counter counts down. For
207 example, a 64-bit free running counter that counts up would have a StartValue
208 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
209 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
211 @param StartValue The value the performance counter starts with when it
213 @param EndValue The value that the performance counter ends with before
216 @return The frequency in Hz.
221 GetPerformanceCounterProperties (
222 OUT UINT64
*StartValue
, OPTIONAL
223 OUT UINT64
*EndValue OPTIONAL
226 if (StartValue
!= NULL
) {
230 if (EndValue
!= NULL
) {
231 *EndValue
= 0xffffffffffffffffULL
;
233 return InternalGetPerformanceCounterFrequency ();
237 Converts elapsed ticks of performance counter to time in nanoseconds.
239 This function converts the elapsed ticks of running performance counter to
240 time value in unit of nanoseconds.
242 @param Ticks The number of elapsed ticks of running performance counter.
244 @return The elapsed time in nanoseconds.
249 GetTimeInNanoSecond (
258 Frequency
= GetPerformanceCounterProperties (NULL
, NULL
);
262 // Time = --------- x 1,000,000,000
265 NanoSeconds
= MultU64x32 (DivU64x64Remainder (Ticks
, Frequency
, &Remainder
), 1000000000u);
268 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
269 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
270 // i.e. highest bit set in Remainder should <= 33.
272 Shift
= MAX (0, HighBitSet64 (Remainder
) - 33);
273 Remainder
= RShiftU64 (Remainder
, (UINTN
) Shift
);
274 Frequency
= RShiftU64 (Frequency
, (UINTN
) Shift
);
275 NanoSeconds
+= DivU64x64Remainder (MultU64x32 (Remainder
, 1000000000u), Frequency
, NULL
);