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