]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/SecPeiDxeTimerLibUefiCpu/X86TimerLib.c
UefiCpuPkg: Add ASSERT to handle local APIC not config properly
[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
f17e2f8c 6 Copyright (c) 2010 - 2015, 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
f17e2f8c
HW
48 This function will ASSERT if the APIC timer intial count returned from\r
49 GetApicTimerInitCount() is zero.\r
50\r
2057d8c8 51 @param Delay A period of time to delay in ticks.\r
52\r
53**/\r
54VOID\r
55EFIAPI\r
56InternalX86Delay (\r
57 IN UINT32 Delay\r
58 )\r
59{\r
60 INT32 Ticks;\r
253fcc8b
JF
61 UINT32 Times;\r
62 UINT32 InitCount;\r
63 UINT32 StartTick;\r
2057d8c8 64\r
65 //\r
253fcc8b
JF
66 // In case Delay is too larger, separate it into several small delay slot.\r
67 // Devided Delay by half value of Init Count is to avoid Delay close to\r
68 // the Init Count, timeout maybe missing if the time consuming between 2\r
69 // GetApicTimerCurrentCount() invoking is larger than the time gap between\r
70 // Delay and the Init Count.\r
2057d8c8 71 //\r
253fcc8b 72 InitCount = GetApicTimerInitCount ();\r
f17e2f8c 73 ASSERT (InitCount != 0);\r
253fcc8b
JF
74 Times = Delay / (InitCount / 2);\r
75 Delay = Delay % (InitCount / 2);\r
2057d8c8 76\r
77 //\r
253fcc8b 78 // Get Start Tick and do delay\r
2057d8c8 79 //\r
253fcc8b
JF
80 StartTick = GetApicTimerCurrentCount ();\r
81 do {\r
82 //\r
83 // Wait until time out by Delay value\r
84 //\r
85 do {\r
86 CpuPause ();\r
87 //\r
88 // Get Ticks from Start to Current.\r
89 //\r
90 Ticks = StartTick - GetApicTimerCurrentCount ();\r
91 //\r
92 // Ticks < 0 means Timer wrap-arounds happens.\r
93 //\r
94 if (Ticks < 0) {\r
95 Ticks += InitCount;\r
96 }\r
97 } while ((UINT32)Ticks < Delay);\r
98\r
99 //\r
100 // Update StartTick and Delay for next delay slot\r
101 //\r
102 StartTick -= (StartTick > Delay) ? Delay : (Delay - InitCount);\r
103 Delay = InitCount / 2;\r
104 } while (Times-- > 0);\r
2057d8c8 105}\r
106\r
107/**\r
108 Stalls the CPU for at least the given number of microseconds.\r
109\r
110 Stalls the CPU for the number of microseconds specified by MicroSeconds.\r
111\r
112 @param MicroSeconds The minimum number of microseconds to delay.\r
113\r
114 @return The value of MicroSeconds inputted.\r
115\r
116**/\r
117UINTN\r
118EFIAPI\r
119MicroSecondDelay (\r
120 IN UINTN MicroSeconds\r
121 )\r
122{\r
123 InternalX86Delay (\r
124 (UINT32)DivU64x32 (\r
125 MultU64x64 (\r
126 InternalX86GetTimerFrequency (),\r
127 MicroSeconds\r
128 ),\r
129 1000000u\r
130 )\r
131 );\r
132 return MicroSeconds;\r
133}\r
134\r
135/**\r
136 Stalls the CPU for at least the given number of nanoseconds.\r
137\r
138 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r
139\r
140 @param NanoSeconds The minimum number of nanoseconds to delay.\r
141\r
142 @return The value of NanoSeconds inputted.\r
143\r
144**/\r
145UINTN\r
146EFIAPI\r
147NanoSecondDelay (\r
148 IN UINTN NanoSeconds\r
149 )\r
150{\r
151 InternalX86Delay (\r
152 (UINT32)DivU64x32 (\r
153 MultU64x64 (\r
154 InternalX86GetTimerFrequency (),\r
155 NanoSeconds\r
156 ),\r
157 1000000000u\r
158 )\r
159 );\r
160 return NanoSeconds;\r
161}\r
162\r
163/**\r
164 Retrieves the current value of a 64-bit free running performance counter.\r
165\r
166 The counter can either count up by 1 or count down by 1. If the physical\r
167 performance counter counts by a larger increment, then the counter values\r
168 must be translated. The properties of the counter can be retrieved from\r
169 GetPerformanceCounterProperties().\r
170\r
171 @return The current value of the free running performance counter.\r
172\r
173**/\r
174UINT64\r
175EFIAPI\r
176GetPerformanceCounter (\r
177 VOID\r
178 )\r
179{\r
180 return (UINT64)GetApicTimerCurrentCount ();\r
181}\r
182\r
183/**\r
184 Retrieves the 64-bit frequency in Hz and the range of performance counter\r
185 values.\r
186\r
187 If StartValue is not NULL, then the value that the performance counter starts\r
188 with immediately after is it rolls over is returned in StartValue. If\r
189 EndValue is not NULL, then the value that the performance counter end with\r
190 immediately before it rolls over is returned in EndValue. The 64-bit\r
191 frequency of the performance counter in Hz is always returned. If StartValue\r
192 is less than EndValue, then the performance counter counts up. If StartValue\r
193 is greater than EndValue, then the performance counter counts down. For\r
194 example, a 64-bit free running counter that counts up would have a StartValue\r
195 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r
196 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r
197\r
198 @param StartValue The value the performance counter starts with when it\r
199 rolls over.\r
200 @param EndValue The value that the performance counter ends with before\r
201 it rolls over.\r
202\r
203 @return The frequency in Hz.\r
204\r
205**/\r
206UINT64\r
207EFIAPI\r
208GetPerformanceCounterProperties (\r
209 OUT UINT64 *StartValue, OPTIONAL\r
210 OUT UINT64 *EndValue OPTIONAL\r
211 )\r
212{\r
213 if (StartValue != NULL) {\r
214 *StartValue = (UINT64)GetApicTimerInitCount ();\r
2057d8c8 215 }\r
216\r
217 if (EndValue != NULL) {\r
218 *EndValue = 0;\r
219 }\r
220\r
221 return (UINT64) InternalX86GetTimerFrequency ();\r
222}\r
b9610b9c 223\r
224/**\r
225 Converts elapsed ticks of performance counter to time in nanoseconds.\r
226\r
227 This function converts the elapsed ticks of running performance counter to\r
228 time value in unit of nanoseconds.\r
229\r
230 @param Ticks The number of elapsed ticks of running performance counter.\r
231\r
232 @return The elapsed time in nanoseconds.\r
233\r
234**/\r
235UINT64\r
236EFIAPI\r
237GetTimeInNanoSecond (\r
238 IN UINT64 Ticks\r
239 )\r
240{\r
241 UINT64 Frequency;\r
242 UINT64 NanoSeconds;\r
243 UINT64 Remainder;\r
244 INTN Shift;\r
245\r
246 Frequency = GetPerformanceCounterProperties (NULL, NULL);\r
247\r
248 //\r
249 // Ticks\r
250 // Time = --------- x 1,000,000,000\r
251 // Frequency\r
252 //\r
253 NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);\r
254\r
255 //\r
256 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.\r
257 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,\r
258 // i.e. highest bit set in Remainder should <= 33.\r
259 //\r
260 Shift = MAX (0, HighBitSet64 (Remainder) - 33);\r
261 Remainder = RShiftU64 (Remainder, (UINTN) Shift);\r
262 Frequency = RShiftU64 (Frequency, (UINTN) Shift);\r
263 NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);\r
264\r
265 return NanoSeconds;\r
266}\r