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