]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/EdkIIGlueLib/Library/BaseTimerLibLocalApic/x86TimerLib.c
1) Sync EdkCompatibilityPkg with EDK 1.04. The changes includes:
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / EdkIIGlueLib / Library / BaseTimerLibLocalApic / x86TimerLib.c
1 /*++
2
3 Copyright (c) 2004 - 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12
13 Module Name:
14
15 x86TimerLib.c
16
17 Abstract:
18
19 Timer Library functions built upon local APIC on IA32/x64.
20
21 @bug Should use PCD to retrieve all the constants including index of
22 the IA32_APIC_BASE MSR, the offsets of InitialCount, CorrentCount
23 and DivideConfiguration.
24
25 --*/
26
27 #include "EdkIIGlueBase.h"
28
29 //
30 // The following array is used in calculating the frequency of local APIC
31 // timer. Refer to IA-32 developers' manual for more details.
32 //
33 GLOBAL_REMOVE_IF_UNREFERENCED
34 CONST UINT8 mTimerLibLocalApicDivisor[] = {
35 0x02, 0x04, 0x08, 0x10,
36 0x02, 0x04, 0x08, 0x10,
37 0x20, 0x40, 0x80, 0x01,
38 0x20, 0x40, 0x80, 0x01
39 };
40
41 VOID
42 CpuInitLocalApicTimer (
43 VOID
44 );
45
46 /**
47 Local Apic Timer Initialize
48 Local Apic Timer needs to be initialized before being used
49 **/
50 EFI_STATUS
51 EFIAPI
52 BaseTimerLibLocalApicInit (
53 VOID
54 )
55 {
56 CpuInitLocalApicTimer();
57
58 return EFI_SUCCESS;
59 }
60
61 /**
62 Internal function to retrieve the base address of local APIC.
63
64 Internal function to retrieve the base address of local APIC.
65
66 @return The base address of local APIC
67
68 **/
69 UINTN
70 InternalX86GetApicBase (
71 VOID
72 )
73 {
74 return (UINTN)AsmMsrBitFieldRead64 (27, 12, 35) << 12;
75 }
76
77 /**
78 Internal function to return the frequency of the local APIC timer.
79
80 Internal function to return the frequency of the local APIC timer.
81
82 @param ApicBase The base address of memory mapped registers of local APIC.
83
84 @return The frequency of the timer in Hz.
85
86 **/
87 STATIC
88 UINT32
89 InternalX86GetTimerFrequency (
90 IN UINTN ApicBase
91 )
92 {
93 return
94 PcdGet32(PcdFSBClock) /
95 mTimerLibLocalApicDivisor[MmioBitFieldRead32 (ApicBase + 0x3e0, 0, 3)];
96 }
97
98 /**
99 Internal function to read the current tick counter of local APIC.
100
101 Internal function to read the current tick counter of local APIC.
102
103 @param ApicBase The base address of memory mapped registers of local APIC.
104
105 @return The tick counter read.
106
107 **/
108 STATIC
109 INT32
110 InternalX86GetTimerTick (
111 IN UINTN ApicBase
112 )
113 {
114 return MmioRead32 (ApicBase + 0x390);
115 }
116
117 /**
118 Stalls the CPU for at least the given number of ticks.
119
120 Stalls the CPU for at least the given number of ticks. It's invoked by
121 MicroSecondDelay() and NanoSecondDelay().
122
123 @param ApicBase The base address of memory mapped registers of local APIC.
124 @param Delay A period of time to delay in ticks.
125
126 **/
127 STATIC
128 VOID
129 InternalX86Delay (
130 IN UINTN ApicBase,
131 IN UINT32 Delay
132 )
133 {
134 INT32 Ticks;
135
136 //
137 // The target timer count is calculated here
138 //
139 Ticks = InternalX86GetTimerTick (ApicBase) - Delay;
140
141 //
142 // Wait until time out
143 // Delay > 2^31 could not be handled by this function
144 // Timer wrap-arounds are handled correctly by this function
145 //
146 while (InternalX86GetTimerTick (ApicBase) - Ticks >= 0);
147 }
148
149 /**
150 Stalls the CPU for at least the given number of microseconds.
151
152 Stalls the CPU for the number of microseconds specified by MicroSeconds.
153
154 @param MicroSeconds The minimum number of microseconds to delay.
155
156 @return MicroSeconds
157
158 **/
159 UINTN
160 EFIAPI
161 MicroSecondDelay (
162 IN UINTN MicroSeconds
163 )
164 {
165 UINTN ApicBase;
166
167 ApicBase = InternalX86GetApicBase ();
168 InternalX86Delay (
169 ApicBase,
170 (UINT32)DivU64x32 (
171 MultU64x64 (
172 InternalX86GetTimerFrequency (ApicBase),
173 MicroSeconds
174 ),
175 1000000u
176 )
177 );
178 return MicroSeconds;
179 }
180
181 /**
182 Stalls the CPU for at least the given number of nanoseconds.
183
184 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
185
186 @param NanoSeconds The minimum number of nanoseconds to delay.
187
188 @return NanoSeconds
189
190 **/
191 UINTN
192 EFIAPI
193 NanoSecondDelay (
194 IN UINTN NanoSeconds
195 )
196 {
197 UINTN ApicBase;
198
199 ApicBase = InternalX86GetApicBase ();
200 InternalX86Delay (
201 ApicBase,
202 (UINT32)DivU64x32 (
203 MultU64x64 (
204 InternalX86GetTimerFrequency (ApicBase),
205 NanoSeconds
206 ),
207 1000000000u
208 )
209 );
210 return NanoSeconds;
211 }
212
213 /**
214 Retrieves the current value of a 64-bit free running performance counter.
215
216 Retrieves the current value of a 64-bit free running performance counter. The
217 counter can either count up by 1 or count down by 1. If the physical
218 performance counter counts by a larger increment, then the counter values
219 must be translated. The properties of the counter can be retrieved from
220 GetPerformanceCounterProperties().
221
222 @return The current value of the free running performance counter.
223
224 **/
225 UINT64
226 EFIAPI
227 GetPerformanceCounter (
228 VOID
229 )
230 {
231 return (UINT64)(UINT32)InternalX86GetTimerTick (InternalX86GetApicBase ());
232 }
233
234 /**
235 Retrieves the 64-bit frequency in Hz and the range of performance counter
236 values.
237
238 If StartValue is not NULL, then the value that the performance counter starts
239 with immediately after is it rolls over is returned in StartValue. If
240 EndValue is not NULL, then the value that the performance counter end with
241 immediately before it rolls over is returned in EndValue. The 64-bit
242 frequency of the performance counter in Hz is always returned. If StartValue
243 is less than EndValue, then the performance counter counts up. If StartValue
244 is greater than EndValue, then the performance counter counts down. For
245 example, a 64-bit free running counter that counts up would have a StartValue
246 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
247 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
248
249 @param StartValue The value the performance counter starts with when it
250 rolls over.
251 @param EndValue The value that the performance counter ends with before
252 it rolls over.
253
254 @return The frequency in Hz.
255
256 **/
257 UINT64
258 EFIAPI
259 GetPerformanceCounterProperties (
260 OUT UINT64 *StartValue, OPTIONAL
261 OUT UINT64 *EndValue OPTIONAL
262 )
263 {
264 UINTN ApicBase;
265
266 ApicBase = InternalX86GetApicBase ();
267
268 if (StartValue != NULL) {
269 *StartValue = MmioRead32 (ApicBase + 0x380);
270 }
271
272 if (EndValue != NULL) {
273 *EndValue = 0;
274 }
275
276 return (UINT64) InternalX86GetTimerFrequency (ApicBase);;
277 }