VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/MdePkg/Library/SecPeiDxeTimerLibCpu/X86TimerLib.c@ 58459

Last change on this file since 58459 was 58459, checked in by vboxsync, 9 years ago

EFI/Firmware: 'svn merge /vendor/edk2/UDK2010.SR1 /vendor/edk2/current .', reverting and removing files+dirs listed in ReadMe.vbox, resolving conflicts with help from ../UDK2014.SP1/. This is a raw untested merge.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette