2 This library implements the Timer Library using the Extended SAL Stall Services Class.
4 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Protocol/ExtendedSalServiceClasses.h>
19 #include <Library/TimerLib.h>
20 #include <Library/BaseLib.h>
21 #include <Library/ExtendedSalLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/PalLib.h>
26 Stalls the CPU for at least the given number of microseconds.
28 This function wraps EsalStall function of Extended SAL Stall Services Class.
29 It stalls the CPU for the number of microseconds specified by MicroSeconds.
31 @param MicroSeconds The minimum number of microseconds to delay.
43 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_LO
,
44 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_HI
,
58 Stalls the CPU for at least the given number of nanoseconds.
60 This function wraps EsalStall function of Extended SAL Stall Services Class.
61 It stalls the CPU for the number of nanoseconds specified by NanoSeconds.
63 @param NanoSeconds The minimum number of nanoseconds to delay.
77 // The unit of ESAL Stall service is microsecond, so we turn the time interval
78 // from nanosecond to microsecond, using the ceiling value to ensure stalling
79 // at least the given number of nanoseconds.
81 MicroSeconds
= DivU64x32 (NanoSeconds
+ 999, 1000);
83 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_LO
,
84 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_HI
,
98 Retrieves the current value of a 64-bit free running performance counter.
100 Retrieves the current value of a 64-bit free running performance counter. The
101 counter can either count up by 1 or count down by 1. If the physical
102 performance counter counts by a larger increment, then the counter values
103 must be translated. The properties of the counter can be retrieved from
104 GetPerformanceCounterProperties().
106 @return The current value of the free running performance counter.
111 GetPerformanceCounter (
115 return AsmReadItc ();
119 Retrieves the 64-bit frequency in Hz and the range of performance counter
122 If StartValue is not NULL, then the value that the performance counter starts
123 with immediately after is it rolls over is returned in StartValue. If
124 EndValue is not NULL, then the value that the performance counter end with
125 immediately before it rolls over is returned in EndValue. The 64-bit
126 frequency of the performance counter in Hz is always returned. If StartValue
127 is less than EndValue, then the performance counter counts up. If StartValue
128 is greater than EndValue, then the performance counter counts down. For
129 example, a 64-bit free running counter that counts up would have a StartValue
130 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
131 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
133 @param StartValue The value the performance counter starts with when it
135 @param EndValue The value that the performance counter ends with before
138 @return The frequency in Hz.
143 GetPerformanceCounterProperties (
144 OUT UINT64
*StartValue
, OPTIONAL
145 OUT UINT64
*EndValue OPTIONAL
148 PAL_CALL_RETURN PalRet
;
149 UINT64 BaseFrequence
;
152 // Get processor base frequency
154 PalRet
= PalCall (PAL_FREQ_BASE
, 0, 0, 0);
155 ASSERT (PalRet
.Status
== 0);
156 BaseFrequence
= PalRet
.r9
;
159 // Get processor frequency ratio
161 PalRet
= PalCall (PAL_FREQ_RATIOS
, 0, 0, 0);
162 ASSERT (PalRet
.Status
== 0);
165 // Start value of counter is 0
167 if (StartValue
!= NULL
) {
172 // End value of counter is 0xFFFFFFFFFFFFFFFF
174 if (EndValue
!= NULL
) {
175 *EndValue
= (UINT64
)(-1);
178 return BaseFrequence
* (PalRet
.r11
>> 32) / (UINT32
)PalRet
.r11
;
182 Converts elapsed ticks of performance counter to time in nanoseconds.
184 This function converts the elapsed ticks of running performance counter to
185 time value in unit of nanoseconds.
187 @param Ticks The number of elapsed ticks of running performance counter.
189 @return The elapsed time in nanoseconds.
194 GetTimeInNanoSecond (
203 Frequency
= GetPerformanceCounterProperties (NULL
, NULL
);
207 // Time = --------- x 1,000,000,000
210 NanoSeconds
= MultU64x32 (DivU64x64Remainder (Ticks
, Frequency
, &Remainder
), 1000000000u);
213 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
214 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
215 // i.e. highest bit set in Remainder should <= 33.
217 Shift
= MAX (0, HighBitSet64 (Remainder
) - 33);
218 Remainder
= RShiftU64 (Remainder
, (UINTN
) Shift
);
219 Frequency
= RShiftU64 (Frequency
, (UINTN
) Shift
);
220 NanoSeconds
+= DivU64x64Remainder (MultU64x32 (Remainder
, 1000000000u), Frequency
, NULL
);