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
= {
17 0xe1ec5ad0, 0x8569, 0x46bd, { 0x8d, 0xcd, 0x3b, 0x9f, 0x6f, 0x45, 0x82, 0x7a }
21 Internal function to retrieves the 64-bit frequency in Hz.
23 Internal function to retrieves the 64-bit frequency in Hz.
25 @return The frequency in Hz.
29 InternalGetPerformanceCounterFrequency (
34 CPUID Leaf 0x15 for Core Crystal Clock Frequency.
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.
42 CpuidCoreClockCalculateTscFrequency (
47 UINT64 CoreXtalFrequency
;
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.
57 AsmCpuid (CPUID_TIME_STAMP_COUNTER
, &RegEax
, &RegEbx
, &RegEcx
, NULL
);
60 // If EAX or EBX returns 0, the XTAL ratio is not enumerated.
62 if ((RegEax
== 0) || (RegEbx
== 0)) {
69 // If ECX returns 0, the XTAL frequency is not enumerated.
70 // And PcdCpuCoreCrystalClockFrequency defined should base on processor series.
73 CoreXtalFrequency
= PcdGet64 (PcdCpuCoreCrystalClockFrequency
);
75 CoreXtalFrequency
= (UINT64
)RegEcx
;
79 // Calculate TSC frequency = (ECX, Core Xtal Frequency) * EBX/EAX
81 TscFrequency
= DivU64x32 (MultU64x32 (CoreXtalFrequency
, RegEbx
) + (UINT64
)(RegEax
>> 1), RegEax
);
87 Stalls the CPU for at least the given number of ticks.
89 Stalls the CPU for at least the given number of ticks. It's invoked by
90 MicroSecondDelay() and NanoSecondDelay().
92 @param Delay A period of time to delay in ticks.
103 // The target timer count is calculated here
105 Ticks
= AsmReadTsc () + Delay
;
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.
113 while (AsmReadTsc () <= Ticks
) {
119 Stalls the CPU for at least the given number of microseconds.
121 Stalls the CPU for the number of microseconds specified by MicroSeconds.
123 @param[in] MicroSeconds The minimum number of microseconds to delay.
131 IN UINTN MicroSeconds
138 InternalGetPerformanceCounterFrequency ()
148 Stalls the CPU for at least the given number of nanoseconds.
150 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
152 @param NanoSeconds The minimum number of nanoseconds to delay.
167 InternalGetPerformanceCounterFrequency ()
177 Retrieves the current value of a 64-bit free running performance counter.
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().
185 @return The current value of the free running performance counter.
190 GetPerformanceCounter (
194 return AsmReadTsc ();
198 Retrieves the 64-bit frequency in Hz and the range of performance counter
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.
212 @param StartValue The value the performance counter starts with when it
214 @param EndValue The value that the performance counter ends with before
217 @return The frequency in Hz.
222 GetPerformanceCounterProperties (
223 OUT UINT64
*StartValue OPTIONAL
,
224 OUT UINT64
*EndValue OPTIONAL
227 if (StartValue
!= NULL
) {
231 if (EndValue
!= NULL
) {
232 *EndValue
= 0xffffffffffffffffULL
;
235 return InternalGetPerformanceCounterFrequency ();
239 Converts elapsed ticks of performance counter to time in nanoseconds.
241 This function converts the elapsed ticks of running performance counter to
242 time value in unit of nanoseconds.
244 @param Ticks The number of elapsed ticks of running performance counter.
246 @return The elapsed time in nanoseconds.
251 GetTimeInNanoSecond (
260 Frequency
= GetPerformanceCounterProperties (NULL
, NULL
);
264 // Time = --------- x 1,000,000,000
267 NanoSeconds
= MultU64x32 (DivU64x64Remainder (Ticks
, Frequency
, &Remainder
), 1000000000u);
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.
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
);