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