]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseTimerLibLocalApic/x86TimerLib.c
Adding a dialog for setting the Build Preferences in the target.txt file.
[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 volatile static UINT64 Mask = 0xffffffff;
214 return InternalX86GetTimerTick (InternalX86GetApicBase ()) & Mask;
215 }
216
217 /**
218 Retrieves the 64-bit frequency in Hz and the range of performance counter
219 values.
220
221 If StartValue is not NULL, then the value that the performance counter starts
222 with immediately after is it rolls over is returned in StartValue. If
223 EndValue is not NULL, then the value that the performance counter end with
224 immediately before it rolls over is returned in EndValue. The 64-bit
225 frequency of the performance counter in Hz is always returned. If StartValue
226 is less than EndValue, then the performance counter counts up. If StartValue
227 is greater than EndValue, then the performance counter counts down. For
228 example, a 64-bit free running counter that counts up would have a StartValue
229 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
230 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
231
232 @param StartValue The value the performance counter starts with when it
233 rolls over.
234 @param EndValue The value that the performance counter ends with before
235 it rolls over.
236
237 @return The frequency in Hz.
238
239 **/
240 UINT64
241 EFIAPI
242 GetPerformanceCounterProperties (
243 IN UINT64 *StartValue,
244 IN UINT64 *EndValue
245 )
246 {
247 UINTN ApicBase;
248
249 ApicBase = InternalX86GetApicBase ();
250
251 if (StartValue != NULL) {
252 *StartValue = MmioRead32 (ApicBase + 0x380);
253 }
254
255 if (EndValue != NULL) {
256 *EndValue = 0;
257 }
258
259 return InternalX86GetTimerFrequency (ApicBase);
260 }