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