1 /** @file 2 This library implements the Timer Library using the Extended SAL Stall Services Class. 3 4 Copyright (c) 2007 - 2011, 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. 9 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. 12 13 **/ 14 15 #include <PiDxe.h> 16 17 #include <Protocol/ExtendedSalServiceClasses.h> 18 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> 24 25 /** 26 Stalls the CPU for at least the given number of microseconds. 27 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. 30 31 @param MicroSeconds The minimum number of microseconds to delay. 32 33 @return MicroSeconds 34 35 **/ 36 UINTN 37 EFIAPI 38 MicroSecondDelay ( 39 IN UINTN MicroSeconds 40 ) 41 { 42 EsalCall ( 43 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_LO, 44 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_HI, 45 StallFunctionId, 46 MicroSeconds, 47 0, 48 0, 49 0, 50 0, 51 0, 52 0 53 ); 54 return MicroSeconds; 55 } 56 57 /** 58 Stalls the CPU for at least the given number of nanoseconds. 59 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. 62 63 @param NanoSeconds The minimum number of nanoseconds to delay. 64 65 @return NanoSeconds 66 67 **/ 68 UINTN 69 EFIAPI 70 NanoSecondDelay ( 71 IN UINTN NanoSeconds 72 ) 73 { 74 UINT64 MicroSeconds; 75 76 // 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. 80 // 81 MicroSeconds = DivU64x32 (NanoSeconds + 999, 1000); 82 EsalCall ( 83 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_LO, 84 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_HI, 85 StallFunctionId, 86 MicroSeconds, 87 0, 88 0, 89 0, 90 0, 91 0, 92 0 93 ); 94 return NanoSeconds; 95 } 96 97 /** 98 Retrieves the current value of a 64-bit free running performance counter. 99 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(). 105 106 @return The current value of the free running performance counter. 107 108 **/ 109 UINT64 110 EFIAPI 111 GetPerformanceCounter ( 112 VOID 113 ) 114 { 115 return AsmReadItc (); 116 } 117 118 /** 119 Retrieves the 64-bit frequency in Hz and the range of performance counter 120 values. 121 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. 132 133 @param StartValue The value the performance counter starts with when it 134 rolls over. 135 @param EndValue The value that the performance counter ends with before 136 it rolls over. 137 138 @return The frequency in Hz. 139 140 **/ 141 UINT64 142 EFIAPI 143 GetPerformanceCounterProperties ( 144 OUT UINT64 *StartValue, OPTIONAL 145 OUT UINT64 *EndValue OPTIONAL 146 ) 147 { 148 PAL_CALL_RETURN PalRet; 149 UINT64 BaseFrequence; 150 151 // 152 // Get processor base frequency 153 // 154 PalRet = PalCall (PAL_FREQ_BASE, 0, 0, 0); 155 ASSERT (PalRet.Status == 0); 156 BaseFrequence = PalRet.r9; 157 158 // 159 // Get processor frequency ratio 160 // 161 PalRet = PalCall (PAL_FREQ_RATIOS, 0, 0, 0); 162 ASSERT (PalRet.Status == 0); 163 164 // 165 // Start value of counter is 0 166 // 167 if (StartValue != NULL) { 168 *StartValue = 0; 169 } 170 171 // 172 // End value of counter is 0xFFFFFFFFFFFFFFFF 173 // 174 if (EndValue != NULL) { 175 *EndValue = (UINT64)(-1); 176 } 177 178 return BaseFrequence * (PalRet.r11 >> 32) / (UINT32)PalRet.r11; 179 } 180 181 /** 182 Converts elapsed ticks of performance counter to time in nanoseconds. 183 184 This function converts the elapsed ticks of running performance counter to 185 time value in unit of nanoseconds. 186 187 @param Ticks The number of elapsed ticks of running performance counter. 188 189 @return The elapsed time in nanoseconds. 190 191 **/ 192 UINT64 193 EFIAPI 194 GetTimeInNanoSecond ( 195 IN UINT64 Ticks 196 ) 197 { 198 UINT64 Frequency; 199 UINT64 NanoSeconds; 200 UINT64 Remainder; 201 INTN Shift; 202 203 Frequency = GetPerformanceCounterProperties (NULL, NULL); 204 205 // 206 // Ticks 207 // Time = --------- x 1,000,000,000 208 // Frequency 209 // 210 NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u); 211 212 // 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. 216 // 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); 221 222 return NanoSeconds; 223 } 224