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