1 /** @file 2 Timer Library functions built upon local APIC on IA32/x64. 3 4 This library uses the local APIC library so that it supports x2APIC mode. 5 6 Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR> 7 This program and the accompanying materials 8 are licensed and made available under the terms and conditions of the BSD License 9 which accompanies this distribution. The full text of the license may be found at 10 http://opensource.org/licenses/bsd-license.php. 11 12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 14 15 **/ 16 17 #include <Base.h> 18 #include <Library/TimerLib.h> 19 #include <Library/BaseLib.h> 20 #include <Library/PcdLib.h> 21 #include <Library/DebugLib.h> 22 #include <Library/LocalApicLib.h> 23 24 /** 25 Internal function to return the frequency of the local APIC timer. 26 27 @return The frequency of the timer in Hz. 28 29 **/ 30 UINT32 31 EFIAPI 32 InternalX86GetTimerFrequency ( 33 VOID 34 ) 35 { 36 UINTN Divisor; 37 38 GetApicTimerState (&Divisor, NULL, NULL); 39 return PcdGet32(PcdFSBClock) / (UINT32)Divisor; 40 } 41 42 /** 43 Stalls the CPU for at least the given number of ticks. 44 45 Stalls the CPU for at least the given number of ticks. It's invoked by 46 MicroSecondDelay() and NanoSecondDelay(). 47 48 This function will ASSERT if the APIC timer intial count returned from 49 GetApicTimerInitCount() is zero. 50 51 @param Delay A period of time to delay in ticks. 52 53 **/ 54 VOID 55 EFIAPI 56 InternalX86Delay ( 57 IN UINT32 Delay 58 ) 59 { 60 INT32 Ticks; 61 UINT32 Times; 62 UINT32 InitCount; 63 UINT32 StartTick; 64 65 // 66 // In case Delay is too larger, separate it into several small delay slot. 67 // Devided Delay by half value of Init Count is to avoid Delay close to 68 // the Init Count, timeout maybe missing if the time consuming between 2 69 // GetApicTimerCurrentCount() invoking is larger than the time gap between 70 // Delay and the Init Count. 71 // 72 InitCount = GetApicTimerInitCount (); 73 ASSERT (InitCount != 0); 74 Times = Delay / (InitCount / 2); 75 Delay = Delay % (InitCount / 2); 76 77 // 78 // Get Start Tick and do delay 79 // 80 StartTick = GetApicTimerCurrentCount (); 81 do { 82 // 83 // Wait until time out by Delay value 84 // 85 do { 86 CpuPause (); 87 // 88 // Get Ticks from Start to Current. 89 // 90 Ticks = StartTick - GetApicTimerCurrentCount (); 91 // 92 // Ticks < 0 means Timer wrap-arounds happens. 93 // 94 if (Ticks < 0) { 95 Ticks += InitCount; 96 } 97 } while ((UINT32)Ticks < Delay); 98 99 // 100 // Update StartTick and Delay for next delay slot 101 // 102 StartTick -= (StartTick > Delay) ? Delay : (Delay - InitCount); 103 Delay = InitCount / 2; 104 } while (Times-- > 0); 105 } 106 107 /** 108 Stalls the CPU for at least the given number of microseconds. 109 110 Stalls the CPU for the number of microseconds specified by MicroSeconds. 111 112 @param MicroSeconds The minimum number of microseconds to delay. 113 114 @return The value of MicroSeconds inputted. 115 116 **/ 117 UINTN 118 EFIAPI 119 MicroSecondDelay ( 120 IN UINTN MicroSeconds 121 ) 122 { 123 InternalX86Delay ( 124 (UINT32)DivU64x32 ( 125 MultU64x64 ( 126 InternalX86GetTimerFrequency (), 127 MicroSeconds 128 ), 129 1000000u 130 ) 131 ); 132 return MicroSeconds; 133 } 134 135 /** 136 Stalls the CPU for at least the given number of nanoseconds. 137 138 Stalls the CPU for the number of nanoseconds specified by NanoSeconds. 139 140 @param NanoSeconds The minimum number of nanoseconds to delay. 141 142 @return The value of NanoSeconds inputted. 143 144 **/ 145 UINTN 146 EFIAPI 147 NanoSecondDelay ( 148 IN UINTN NanoSeconds 149 ) 150 { 151 InternalX86Delay ( 152 (UINT32)DivU64x32 ( 153 MultU64x64 ( 154 InternalX86GetTimerFrequency (), 155 NanoSeconds 156 ), 157 1000000000u 158 ) 159 ); 160 return NanoSeconds; 161 } 162 163 /** 164 Retrieves the current value of a 64-bit free running performance counter. 165 166 The counter can either count up by 1 or count down by 1. If the physical 167 performance counter counts by a larger increment, then the counter values 168 must be translated. The properties of the counter can be retrieved from 169 GetPerformanceCounterProperties(). 170 171 @return The current value of the free running performance counter. 172 173 **/ 174 UINT64 175 EFIAPI 176 GetPerformanceCounter ( 177 VOID 178 ) 179 { 180 return (UINT64)GetApicTimerCurrentCount (); 181 } 182 183 /** 184 Retrieves the 64-bit frequency in Hz and the range of performance counter 185 values. 186 187 If StartValue is not NULL, then the value that the performance counter starts 188 with immediately after is it rolls over is returned in StartValue. If 189 EndValue is not NULL, then the value that the performance counter end with 190 immediately before it rolls over is returned in EndValue. The 64-bit 191 frequency of the performance counter in Hz is always returned. If StartValue 192 is less than EndValue, then the performance counter counts up. If StartValue 193 is greater than EndValue, then the performance counter counts down. For 194 example, a 64-bit free running counter that counts up would have a StartValue 195 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter 196 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0. 197 198 @param StartValue The value the performance counter starts with when it 199 rolls over. 200 @param EndValue The value that the performance counter ends with before 201 it rolls over. 202 203 @return The frequency in Hz. 204 205 **/ 206 UINT64 207 EFIAPI 208 GetPerformanceCounterProperties ( 209 OUT UINT64 *StartValue, OPTIONAL 210 OUT UINT64 *EndValue OPTIONAL 211 ) 212 { 213 if (StartValue != NULL) { 214 *StartValue = (UINT64)GetApicTimerInitCount (); 215 } 216 217 if (EndValue != NULL) { 218 *EndValue = 0; 219 } 220 221 return (UINT64) InternalX86GetTimerFrequency (); 222 } 223 224 /** 225 Converts elapsed ticks of performance counter to time in nanoseconds. 226 227 This function converts the elapsed ticks of running performance counter to 228 time value in unit of nanoseconds. 229 230 @param Ticks The number of elapsed ticks of running performance counter. 231 232 @return The elapsed time in nanoseconds. 233 234 **/ 235 UINT64 236 EFIAPI 237 GetTimeInNanoSecond ( 238 IN UINT64 Ticks 239 ) 240 { 241 UINT64 Frequency; 242 UINT64 NanoSeconds; 243 UINT64 Remainder; 244 INTN Shift; 245 246 Frequency = GetPerformanceCounterProperties (NULL, NULL); 247 248 // 249 // Ticks 250 // Time = --------- x 1,000,000,000 251 // Frequency 252 // 253 NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u); 254 255 // 256 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit. 257 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34, 258 // i.e. highest bit set in Remainder should <= 33. 259 // 260 Shift = MAX (0, HighBitSet64 (Remainder) - 33); 261 Remainder = RShiftU64 (Remainder, (UINTN) Shift); 262 Frequency = RShiftU64 (Frequency, (UINTN) Shift); 263 NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL); 264 265 return NanoSeconds; 266 } 267