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