]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseTimerLibLocalApic/x86TimerLib.c
e3af546367e10a4b68e53d621c0d2e71723f04d6
[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 ApicBase = InternalX86GetApicBase ();
119
120 //
121 // The target timer count is calculated here
122 //
123 Ticks = InternalX86GetTimerTick (ApicBase) - Delay;
124
125 //
126 // Wait until time out
127 // Delay > 2^31 could not be handled by this function
128 // Timer wrap-arounds are handled correctly by this function
129 //
130 while (InternalX86GetTimerTick (ApicBase) - Ticks >= 0);
131 }
132
133 /**
134 Stalls the CPU for at least the given number of microseconds.
135
136 Stalls the CPU for the number of microseconds specified by MicroSeconds.
137
138 @param MicroSeconds The minimum number of microseconds to delay.
139
140 @return MicroSeconds
141
142 **/
143 UINTN
144 EFIAPI
145 MicroSecondDelay (
146 IN UINTN MicroSeconds
147 )
148 {
149 UINTN ApicBase;
150
151 ApicBase = InternalX86GetApicBase ();
152 InternalX86Delay (
153 ApicBase,
154 (UINT32)DivU64x32 (
155 MultU64x64 (
156 InternalX86GetTimerFrequency (ApicBase),
157 MicroSeconds
158 ),
159 1000000u
160 )
161 );
162 return MicroSeconds;
163 }
164
165 /**
166 Stalls the CPU for at least the given number of nanoseconds.
167
168 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
169
170 @param NanoSeconds The minimum number of nanoseconds to delay.
171
172 @return NanoSeconds
173
174 **/
175 UINTN
176 EFIAPI
177 NanoSecondDelay (
178 IN UINTN NanoSeconds
179 )
180 {
181 UINTN ApicBase;
182
183 ApicBase = InternalX86GetApicBase ();
184 InternalX86Delay (
185 ApicBase,
186 (UINT32)DivU64x32 (
187 MultU64x64 (
188 InternalX86GetTimerFrequency (ApicBase),
189 NanoSeconds
190 ),
191 1000000000u
192 )
193 );
194 return NanoSeconds;
195 }
196
197 /**
198 Retrieves the current value of a 64-bit free running performance counter.
199
200 Retrieves the current value of a 64-bit free running performance counter. The
201 counter can either count up by 1 or count down by 1. If the physical
202 performance counter counts by a larger increment, then the counter values
203 must be translated. The properties of the counter can be retrieved from
204 GetPerformanceCounterProperties().
205
206 @return The current value of the free running performance counter.
207
208 **/
209 UINT64
210 EFIAPI
211 GetPerformanceCounter (
212 VOID
213 )
214 {
215 return InternalX86GetTimerTick (InternalX86GetApicBase ());
216 }
217
218 /**
219 Retrieves the 64-bit frequency in Hz and the range of performance counter
220 values.
221
222 If StartValue is not NULL, then the value that the performance counter starts
223 with immediately after is it rolls over is returned in StartValue. If
224 EndValue is not NULL, then the value that the performance counter end with
225 immediately before it rolls over is returned in EndValue. The 64-bit
226 frequency of the performance counter in Hz is always returned. If StartValue
227 is less than EndValue, then the performance counter counts up. If StartValue
228 is greater than EndValue, then the performance counter counts down. For
229 example, a 64-bit free running counter that counts up would have a StartValue
230 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
231 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
232
233 @param StartValue The value the performance counter starts with when it
234 rolls over.
235 @param EndValue The value that the performance counter ends with before
236 it rolls over.
237
238 @return The frequency in Hz.
239
240 **/
241 UINT64
242 EFIAPI
243 GetPerformanceCounterProperties (
244 IN UINT64 *StartValue,
245 IN UINT64 *EndValue
246 )
247 {
248 UINTN ApicBase;
249
250 ApicBase = InternalX86GetApicBase ();
251
252 if (StartValue != NULL) {
253 *StartValue = MmioRead32 (ApicBase + 0x380);
254 }
255
256 if (EndValue != NULL) {
257 *EndValue = 0;
258 }
259
260 return InternalX86GetTimerFrequency (ApicBase);
261 }