]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdePkg/Library/SecPeiDxeTimerLibCpu/X86TimerLib.c
Removed the assumption on APIC timer initial Count is all 1s and updated it to handle...
[mirror_edk2.git] / MdePkg / Library / SecPeiDxeTimerLibCpu / X86TimerLib.c
... / ...
CommitLineData
1/** @file\r
2 Timer Library functions built upon local APIC on IA32/x64.\r
3\r
4 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <Base.h>\r
16#include <Library/TimerLib.h>\r
17#include <Library/BaseLib.h>\r
18#include <Library/IoLib.h>\r
19#include <Library/PcdLib.h>\r
20#include <Library/DebugLib.h>\r
21\r
22#define APIC_LVTERR 0x370\r
23#define APIC_TMICT 0x380\r
24#define APIC_TMCCT 0x390\r
25#define APIC_TDCR 0x3e0\r
26\r
27//\r
28// The following array is used in calculating the frequency of local APIC\r
29// timer. Refer to IA-32 developers' manual for more details.\r
30//\r
31GLOBAL_REMOVE_IF_UNREFERENCED\r
32CONST UINT8 mTimerLibLocalApicDivisor[] = {\r
33 0x02, 0x04, 0x08, 0x10,\r
34 0x02, 0x04, 0x08, 0x10,\r
35 0x20, 0x40, 0x80, 0x01,\r
36 0x20, 0x40, 0x80, 0x01\r
37};\r
38\r
39/**\r
40 Internal function to retrieve the base address of local APIC.\r
41\r
42 @return The base address of local APIC\r
43\r
44**/\r
45UINTN\r
46EFIAPI\r
47InternalX86GetApicBase (\r
48 VOID\r
49 )\r
50{\r
51 return (UINTN)AsmMsrBitFieldRead64 (27, 12, 35) << 12;\r
52}\r
53\r
54/**\r
55 Internal function to return the frequency of the local APIC timer.\r
56\r
57 @param ApicBase The base address of memory mapped registers of local APIC.\r
58\r
59 @return The frequency of the timer in Hz.\r
60\r
61**/\r
62UINT32\r
63EFIAPI\r
64InternalX86GetTimerFrequency (\r
65 IN UINTN ApicBase\r
66 )\r
67{\r
68 return\r
69 PcdGet32(PcdFSBClock) /\r
70 mTimerLibLocalApicDivisor[MmioBitFieldRead32 (ApicBase + APIC_TDCR, 0, 3)];\r
71}\r
72\r
73/**\r
74 Internal function to read the current tick counter of local APIC.\r
75\r
76 @param ApicBase The base address of memory mapped registers of local APIC.\r
77\r
78 @return The tick counter read.\r
79\r
80**/\r
81INT32\r
82EFIAPI\r
83InternalX86GetTimerTick (\r
84 IN UINTN ApicBase\r
85 )\r
86{\r
87 return MmioRead32 (ApicBase + APIC_TMCCT);\r
88}\r
89\r
90/**\r
91 Internal function to read the initial timer count of local APIC.\r
92\r
93 @param ApicBase The base address of memory mapped registers of local APIC.\r
94\r
95 @return The initial timer count read.\r
96\r
97**/\r
98UINT32\r
99InternalX86GetInitTimerCount (\r
100 IN UINTN ApicBase\r
101 )\r
102{\r
103 return MmioRead32 (ApicBase + APIC_TMICT);\r
104}\r
105\r
106/**\r
107 Stalls the CPU for at least the given number of ticks.\r
108\r
109 Stalls the CPU for at least the given number of ticks. It's invoked by\r
110 MicroSecondDelay() and NanoSecondDelay().\r
111\r
112 @param ApicBase The base address of memory mapped registers of local APIC.\r
113 @param Delay A period of time to delay in ticks.\r
114\r
115**/\r
116VOID\r
117EFIAPI\r
118InternalX86Delay (\r
119 IN UINTN ApicBase,\r
120 IN UINT32 Delay\r
121 )\r
122{\r
123 INT32 Ticks;\r
124 UINT32 Times;\r
125 UINT32 InitCount;\r
126 UINT32 StartTick;\r
127\r
128 //\r
129 // In case Delay is too larger, separate it into several small delay slot.\r
130 // Devided Delay by half value of Init Count is to avoid Delay close to\r
131 // the Init Count, timeout maybe missing if the time consuming between 2\r
132 // GetApicTimerCurrentCount() invoking is larger than the time gap between\r
133 // Delay and the Init Count.\r
134 //\r
135 InitCount = InternalX86GetInitTimerCount (ApicBase);\r
136 Times = Delay / (InitCount / 2);\r
137 Delay = Delay % (InitCount / 2);\r
138\r
139 //\r
140 // Get Start Tick and do delay\r
141 //\r
142 StartTick = InternalX86GetTimerTick (ApicBase);\r
143 do {\r
144 //\r
145 // Wait until time out by Delay value\r
146 //\r
147 do {\r
148 CpuPause ();\r
149 //\r
150 // Get Ticks from Start to Current.\r
151 //\r
152 Ticks = StartTick - InternalX86GetTimerTick (ApicBase);\r
153 //\r
154 // Ticks < 0 means Timer wrap-arounds happens.\r
155 //\r
156 if (Ticks < 0) {\r
157 Ticks += InitCount;\r
158 }\r
159 } while ((UINT32)Ticks < Delay);\r
160\r
161 //\r
162 // Update StartTick and Delay for next delay slot\r
163 //\r
164 StartTick -= (StartTick > Delay) ? Delay : (Delay - InitCount);\r
165 Delay = InitCount / 2;\r
166 } while (Times-- > 0);\r
167}\r
168\r
169/**\r
170 Stalls the CPU for at least the given number of microseconds.\r
171\r
172 Stalls the CPU for the number of microseconds specified by MicroSeconds.\r
173\r
174 @param MicroSeconds The minimum number of microseconds to delay.\r
175\r
176 @return The value of MicroSeconds inputted.\r
177\r
178**/\r
179UINTN\r
180EFIAPI\r
181MicroSecondDelay (\r
182 IN UINTN MicroSeconds\r
183 )\r
184{\r
185 UINTN ApicBase;\r
186\r
187 ApicBase = InternalX86GetApicBase ();\r
188 InternalX86Delay (\r
189 ApicBase,\r
190 (UINT32)DivU64x32 (\r
191 MultU64x64 (\r
192 InternalX86GetTimerFrequency (ApicBase),\r
193 MicroSeconds\r
194 ),\r
195 1000000u\r
196 )\r
197 );\r
198 return MicroSeconds;\r
199}\r
200\r
201/**\r
202 Stalls the CPU for at least the given number of nanoseconds.\r
203\r
204 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r
205\r
206 @param NanoSeconds The minimum number of nanoseconds to delay.\r
207\r
208 @return The value of NanoSeconds inputted.\r
209\r
210**/\r
211UINTN\r
212EFIAPI\r
213NanoSecondDelay (\r
214 IN UINTN NanoSeconds\r
215 )\r
216{\r
217 UINTN ApicBase;\r
218\r
219 ApicBase = InternalX86GetApicBase ();\r
220 InternalX86Delay (\r
221 ApicBase,\r
222 (UINT32)DivU64x32 (\r
223 MultU64x64 (\r
224 InternalX86GetTimerFrequency (ApicBase),\r
225 NanoSeconds\r
226 ),\r
227 1000000000u\r
228 )\r
229 );\r
230 return NanoSeconds;\r
231}\r
232\r
233/**\r
234 Retrieves the current value of a 64-bit free running performance counter.\r
235\r
236 The counter can either count up by 1 or count down by 1. If the physical\r
237 performance counter counts by a larger increment, then the counter values\r
238 must be translated. The properties of the counter can be retrieved from\r
239 GetPerformanceCounterProperties().\r
240\r
241 @return The current value of the free running performance counter.\r
242\r
243**/\r
244UINT64\r
245EFIAPI\r
246GetPerformanceCounter (\r
247 VOID\r
248 )\r
249{\r
250 return (UINT64)(UINT32)InternalX86GetTimerTick (InternalX86GetApicBase ());\r
251}\r
252\r
253/**\r
254 Retrieves the 64-bit frequency in Hz and the range of performance counter\r
255 values.\r
256\r
257 If StartValue is not NULL, then the value that the performance counter starts\r
258 with immediately after is it rolls over is returned in StartValue. If\r
259 EndValue is not NULL, then the value that the performance counter end with\r
260 immediately before it rolls over is returned in EndValue. The 64-bit\r
261 frequency of the performance counter in Hz is always returned. If StartValue\r
262 is less than EndValue, then the performance counter counts up. If StartValue\r
263 is greater than EndValue, then the performance counter counts down. For\r
264 example, a 64-bit free running counter that counts up would have a StartValue\r
265 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r
266 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r
267\r
268 @param StartValue The value the performance counter starts with when it\r
269 rolls over.\r
270 @param EndValue The value that the performance counter ends with before\r
271 it rolls over.\r
272\r
273 @return The frequency in Hz.\r
274\r
275**/\r
276UINT64\r
277EFIAPI\r
278GetPerformanceCounterProperties (\r
279 OUT UINT64 *StartValue, OPTIONAL\r
280 OUT UINT64 *EndValue OPTIONAL\r
281 )\r
282{\r
283 UINTN ApicBase;\r
284\r
285 ApicBase = InternalX86GetApicBase ();\r
286\r
287 if (StartValue != NULL) {\r
288 *StartValue = (UINT64)InternalX86GetInitTimerCount (ApicBase);\r
289 }\r
290\r
291 if (EndValue != NULL) {\r
292 *EndValue = 0;\r
293 }\r
294\r
295 return (UINT64) InternalX86GetTimerFrequency (ApicBase);\r
296}\r
297\r
298/**\r
299 Converts elapsed ticks of performance counter to time in nanoseconds.\r
300\r
301 This function converts the elapsed ticks of running performance counter to\r
302 time value in unit of nanoseconds.\r
303\r
304 @param Ticks The number of elapsed ticks of running performance counter.\r
305\r
306 @return The elapsed time in nanoseconds.\r
307\r
308**/\r
309UINT64\r
310EFIAPI\r
311GetTimeInNanoSecond (\r
312 IN UINT64 Ticks\r
313 )\r
314{\r
315 UINT64 Frequency;\r
316 UINT64 NanoSeconds;\r
317 UINT64 Remainder;\r
318 INTN Shift;\r
319\r
320 Frequency = GetPerformanceCounterProperties (NULL, NULL);\r
321\r
322 //\r
323 // Ticks\r
324 // Time = --------- x 1,000,000,000\r
325 // Frequency\r
326 //\r
327 NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);\r
328\r
329 //\r
330 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.\r
331 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,\r
332 // i.e. highest bit set in Remainder should <= 33.\r
333 //\r
334 Shift = MAX (0, HighBitSet64 (Remainder) - 33);\r
335 Remainder = RShiftU64 (Remainder, (UINTN) Shift);\r
336 Frequency = RShiftU64 (Frequency, (UINTN) Shift);\r
337 NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);\r
338\r
339 return NanoSeconds;\r
340}\r