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