]>
Commit | Line | Data |
---|---|---|
83d1ffb9 LG |
1 | /** @file\r |
2 | ACPI Timer implements one instance of Timer Library.\r | |
3 | \r | |
fd501a79 | 4 | Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>\r |
e1d302e5 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
83d1ffb9 LG |
6 | \r |
7 | **/\r | |
8 | \r | |
9 | #include <Base.h>\r | |
10 | #include <Library/TimerLib.h>\r | |
11 | #include <Library/BaseLib.h>\r | |
12 | #include <Library/PcdLib.h>\r | |
13 | #include <Library/PciLib.h>\r | |
14 | #include <Library/IoLib.h>\r | |
15 | #include <Library/DebugLib.h>\r | |
16 | #include <IndustryStandard/Acpi.h>\r | |
17 | \r | |
fd501a79 LG |
18 | GUID mFrequencyHobGuid = { 0x3fca54f6, 0xe1a2, 0x4b20, { 0xbe, 0x76, 0x92, 0x6b, 0x4b, 0x48, 0xbf, 0xaa }};\r |
19 | \r | |
83d1ffb9 LG |
20 | /**\r |
21 | Internal function to retrieves the 64-bit frequency in Hz.\r | |
22 | \r | |
23 | Internal function to retrieves the 64-bit frequency in Hz.\r | |
24 | \r | |
25 | @return The frequency in Hz.\r | |
26 | \r | |
27 | **/\r | |
28 | UINT64\r | |
29 | InternalGetPerformanceCounterFrequency (\r | |
30 | VOID\r | |
31 | );\r | |
32 | \r | |
33 | /**\r | |
34 | The constructor function enables ACPI IO space.\r | |
35 | \r | |
36 | If ACPI I/O space not enabled, this function will enable it.\r | |
37 | It will always return RETURN_SUCCESS.\r | |
38 | \r | |
39 | @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.\r | |
40 | \r | |
41 | **/\r | |
42 | RETURN_STATUS\r | |
43 | EFIAPI\r | |
44 | AcpiTimerLibConstructor (\r | |
45 | VOID\r | |
46 | )\r | |
47 | {\r | |
48 | UINTN Bus;\r | |
49 | UINTN Device;\r | |
50 | UINTN Function;\r | |
51 | UINTN EnableRegister;\r | |
52 | UINT8 EnableMask;\r | |
53 | \r | |
54 | //\r | |
5a702acd | 55 | // ASSERT for the invalid PCD values. They must be configured to the real value.\r |
83d1ffb9 LG |
56 | //\r |
57 | ASSERT (PcdGet16 (PcdAcpiIoPciBarRegisterOffset) != 0xFFFF);\r | |
58 | ASSERT (PcdGet16 (PcdAcpiIoPortBaseAddress) != 0xFFFF);\r | |
59 | \r | |
60 | //\r | |
5a702acd | 61 | // If the register offset to the BAR for the ACPI I/O Port Base Address is 0x0000, then\r |
83d1ffb9 LG |
62 | // no PCI register programming is required to enable access to the the ACPI registers\r |
63 | // specified by PcdAcpiIoPortBaseAddress\r | |
64 | //\r | |
65 | if (PcdGet16 (PcdAcpiIoPciBarRegisterOffset) == 0x0000) {\r | |
66 | return RETURN_SUCCESS;\r | |
67 | }\r | |
68 | \r | |
69 | //\r | |
5a702acd | 70 | // ASSERT for the invalid PCD values. They must be configured to the real value.\r |
83d1ffb9 LG |
71 | //\r |
72 | ASSERT (PcdGet8 (PcdAcpiIoPciDeviceNumber) != 0xFF);\r | |
73 | ASSERT (PcdGet8 (PcdAcpiIoPciFunctionNumber) != 0xFF);\r | |
74 | ASSERT (PcdGet16 (PcdAcpiIoPciEnableRegisterOffset) != 0xFFFF);\r | |
75 | \r | |
76 | //\r | |
77 | // Retrieve the PCD values for the PCI configuration space required to program the ACPI I/O Port Base Address\r | |
78 | //\r | |
79 | Bus = PcdGet8 (PcdAcpiIoPciBusNumber);\r | |
80 | Device = PcdGet8 (PcdAcpiIoPciDeviceNumber);\r | |
81 | Function = PcdGet8 (PcdAcpiIoPciFunctionNumber);\r | |
82 | EnableRegister = PcdGet16 (PcdAcpiIoPciEnableRegisterOffset);\r | |
83 | EnableMask = PcdGet8 (PcdAcpiIoBarEnableMask);\r | |
84 | \r | |
85 | //\r | |
86 | // If ACPI I/O space is not enabled yet, program ACPI I/O base address and enable it.\r | |
87 | //\r | |
dde4aedc | 88 | if ((PciRead8 (PCI_LIB_ADDRESS (Bus, Device, Function, EnableRegister)) & EnableMask) != EnableMask) {\r |
83d1ffb9 LG |
89 | PciWrite16 (\r |
90 | PCI_LIB_ADDRESS (Bus, Device, Function, PcdGet16 (PcdAcpiIoPciBarRegisterOffset)),\r | |
91 | PcdGet16 (PcdAcpiIoPortBaseAddress)\r | |
92 | );\r | |
93 | PciOr8 (\r | |
94 | PCI_LIB_ADDRESS (Bus, Device, Function, EnableRegister),\r | |
95 | EnableMask\r | |
96 | );\r | |
97 | }\r | |
5a702acd | 98 | \r |
83d1ffb9 LG |
99 | return RETURN_SUCCESS;\r |
100 | }\r | |
101 | \r | |
102 | /**\r | |
103 | Internal function to retrieve the ACPI I/O Port Base Address.\r | |
104 | \r | |
105 | Internal function to retrieve the ACPI I/O Port Base Address.\r | |
106 | \r | |
107 | @return The 16-bit ACPI I/O Port Base Address.\r | |
108 | \r | |
109 | **/\r | |
110 | UINT16\r | |
111 | InternalAcpiGetAcpiTimerIoPort (\r | |
112 | VOID\r | |
113 | )\r | |
114 | {\r | |
115 | UINT16 Port;\r | |
5a702acd | 116 | \r |
9ff926d6 | 117 | Port = PcdGet16 (PcdAcpiIoPortBaseAddress);\r |
5a702acd | 118 | \r |
83d1ffb9 | 119 | //\r |
5a702acd LG |
120 | // If the register offset to the BAR for the ACPI I/O Port Base Address is not 0x0000, then\r |
121 | // read the PCI register for the ACPI BAR value in case the BAR has been programmed to a\r | |
83d1ffb9 LG |
122 | // value other than PcdAcpiIoPortBaseAddress\r |
123 | //\r | |
124 | if (PcdGet16 (PcdAcpiIoPciBarRegisterOffset) != 0x0000) {\r | |
125 | Port = PciRead16 (PCI_LIB_ADDRESS (\r | |
5a702acd LG |
126 | PcdGet8 (PcdAcpiIoPciBusNumber),\r |
127 | PcdGet8 (PcdAcpiIoPciDeviceNumber),\r | |
128 | PcdGet8 (PcdAcpiIoPciFunctionNumber),\r | |
83d1ffb9 LG |
129 | PcdGet16 (PcdAcpiIoPciBarRegisterOffset)\r |
130 | ));\r | |
131 | }\r | |
5a702acd | 132 | \r |
9ff926d6 | 133 | return (Port & PcdGet16 (PcdAcpiIoPortBaseAddressMask)) + PcdGet16 (PcdAcpiPm1TmrOffset);\r |
83d1ffb9 LG |
134 | }\r |
135 | \r | |
136 | /**\r | |
137 | Stalls the CPU for at least the given number of ticks.\r | |
138 | \r | |
139 | Stalls the CPU for at least the given number of ticks. It's invoked by\r | |
140 | MicroSecondDelay() and NanoSecondDelay().\r | |
141 | \r | |
142 | @param Delay A period of time to delay in ticks.\r | |
143 | \r | |
144 | **/\r | |
145 | VOID\r | |
146 | InternalAcpiDelay (\r | |
147 | IN UINT32 Delay\r | |
148 | )\r | |
149 | {\r | |
150 | UINT16 Port;\r | |
151 | UINT32 Ticks;\r | |
152 | UINT32 Times;\r | |
153 | \r | |
154 | Port = InternalAcpiGetAcpiTimerIoPort ();\r | |
155 | Times = Delay >> 22;\r | |
156 | Delay &= BIT22 - 1;\r | |
157 | do {\r | |
158 | //\r | |
159 | // The target timer count is calculated here\r | |
160 | //\r | |
b3b58d4d | 161 | Ticks = IoBitFieldRead32 (Port, 0, 23) + Delay;\r |
83d1ffb9 LG |
162 | Delay = BIT22;\r |
163 | //\r | |
164 | // Wait until time out\r | |
165 | // Delay >= 2^23 could not be handled by this function\r | |
166 | // Timer wrap-arounds are handled correctly by this function\r | |
167 | //\r | |
b3b58d4d | 168 | while (((Ticks - IoBitFieldRead32 (Port, 0, 23)) & BIT23) == 0) {\r |
83d1ffb9 LG |
169 | CpuPause ();\r |
170 | }\r | |
171 | } while (Times-- > 0);\r | |
172 | }\r | |
173 | \r | |
174 | /**\r | |
175 | Stalls the CPU for at least the given number of microseconds.\r | |
176 | \r | |
177 | Stalls the CPU for the number of microseconds specified by MicroSeconds.\r | |
178 | \r | |
179 | @param MicroSeconds The minimum number of microseconds to delay.\r | |
180 | \r | |
181 | @return MicroSeconds\r | |
182 | \r | |
183 | **/\r | |
184 | UINTN\r | |
185 | EFIAPI\r | |
186 | MicroSecondDelay (\r | |
187 | IN UINTN MicroSeconds\r | |
188 | )\r | |
189 | {\r | |
190 | InternalAcpiDelay (\r | |
191 | (UINT32)DivU64x32 (\r | |
192 | MultU64x32 (\r | |
193 | MicroSeconds,\r | |
194 | ACPI_TIMER_FREQUENCY\r | |
195 | ),\r | |
196 | 1000000u\r | |
197 | )\r | |
198 | );\r | |
199 | return MicroSeconds;\r | |
200 | }\r | |
201 | \r | |
202 | /**\r | |
203 | Stalls the CPU for at least the given number of nanoseconds.\r | |
204 | \r | |
205 | Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r | |
206 | \r | |
207 | @param NanoSeconds The minimum number of nanoseconds to delay.\r | |
208 | \r | |
209 | @return NanoSeconds\r | |
210 | \r | |
211 | **/\r | |
212 | UINTN\r | |
213 | EFIAPI\r | |
214 | NanoSecondDelay (\r | |
215 | IN UINTN NanoSeconds\r | |
216 | )\r | |
217 | {\r | |
218 | InternalAcpiDelay (\r | |
219 | (UINT32)DivU64x32 (\r | |
220 | MultU64x32 (\r | |
221 | NanoSeconds,\r | |
222 | ACPI_TIMER_FREQUENCY\r | |
223 | ),\r | |
224 | 1000000000u\r | |
225 | )\r | |
226 | );\r | |
227 | return NanoSeconds;\r | |
228 | }\r | |
229 | \r | |
230 | /**\r | |
231 | Retrieves the current value of a 64-bit free running performance counter.\r | |
232 | \r | |
233 | Retrieves the current value of a 64-bit free running performance counter. The\r | |
234 | counter can either count up by 1 or count down by 1. If the physical\r | |
235 | performance counter counts by a larger increment, then the counter values\r | |
236 | must be translated. The properties of the counter can be retrieved from\r | |
237 | GetPerformanceCounterProperties().\r | |
238 | \r | |
239 | @return The current value of the free running performance counter.\r | |
240 | \r | |
241 | **/\r | |
242 | UINT64\r | |
243 | EFIAPI\r | |
244 | GetPerformanceCounter (\r | |
245 | VOID\r | |
246 | )\r | |
247 | {\r | |
248 | return AsmReadTsc ();\r | |
249 | }\r | |
250 | \r | |
251 | /**\r | |
252 | Retrieves the 64-bit frequency in Hz and the range of performance counter\r | |
253 | values.\r | |
254 | \r | |
255 | If StartValue is not NULL, then the value that the performance counter starts\r | |
256 | with immediately after is it rolls over is returned in StartValue. If\r | |
257 | EndValue is not NULL, then the value that the performance counter end with\r | |
258 | immediately before it rolls over is returned in EndValue. The 64-bit\r | |
259 | frequency of the performance counter in Hz is always returned. If StartValue\r | |
260 | is less than EndValue, then the performance counter counts up. If StartValue\r | |
261 | is greater than EndValue, then the performance counter counts down. For\r | |
262 | example, a 64-bit free running counter that counts up would have a StartValue\r | |
263 | of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r | |
264 | that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r | |
265 | \r | |
266 | @param StartValue The value the performance counter starts with when it\r | |
267 | rolls over.\r | |
268 | @param EndValue The value that the performance counter ends with before\r | |
269 | it rolls over.\r | |
270 | \r | |
271 | @return The frequency in Hz.\r | |
272 | \r | |
273 | **/\r | |
274 | UINT64\r | |
275 | EFIAPI\r | |
276 | GetPerformanceCounterProperties (\r | |
277 | OUT UINT64 *StartValue, OPTIONAL\r | |
278 | OUT UINT64 *EndValue OPTIONAL\r | |
279 | )\r | |
280 | {\r | |
281 | if (StartValue != NULL) {\r | |
282 | *StartValue = 0;\r | |
283 | }\r | |
284 | \r | |
285 | if (EndValue != NULL) {\r | |
286 | *EndValue = 0xffffffffffffffffULL;\r | |
287 | }\r | |
288 | return InternalGetPerformanceCounterFrequency ();\r | |
289 | }\r | |
290 | \r | |
291 | /**\r | |
292 | Converts elapsed ticks of performance counter to time in nanoseconds.\r | |
293 | \r | |
294 | This function converts the elapsed ticks of running performance counter to\r | |
295 | time value in unit of nanoseconds.\r | |
296 | \r | |
297 | @param Ticks The number of elapsed ticks of running performance counter.\r | |
298 | \r | |
299 | @return The elapsed time in nanoseconds.\r | |
300 | \r | |
301 | **/\r | |
302 | UINT64\r | |
303 | EFIAPI\r | |
304 | GetTimeInNanoSecond (\r | |
305 | IN UINT64 Ticks\r | |
306 | )\r | |
307 | {\r | |
308 | UINT64 Frequency;\r | |
309 | UINT64 NanoSeconds;\r | |
310 | UINT64 Remainder;\r | |
311 | INTN Shift;\r | |
312 | \r | |
313 | Frequency = GetPerformanceCounterProperties (NULL, NULL);\r | |
314 | \r | |
315 | //\r | |
316 | // Ticks\r | |
317 | // Time = --------- x 1,000,000,000\r | |
318 | // Frequency\r | |
319 | //\r | |
320 | NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);\r | |
321 | \r | |
322 | //\r | |
323 | // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.\r | |
324 | // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,\r | |
325 | // i.e. highest bit set in Remainder should <= 33.\r | |
326 | //\r | |
327 | Shift = MAX (0, HighBitSet64 (Remainder) - 33);\r | |
328 | Remainder = RShiftU64 (Remainder, (UINTN) Shift);\r | |
329 | Frequency = RShiftU64 (Frequency, (UINTN) Shift);\r | |
330 | NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);\r | |
331 | \r | |
332 | return NanoSeconds;\r | |
333 | }\r | |
62b8b5be SZ |
334 | \r |
335 | /**\r | |
336 | Calculate TSC frequency.\r | |
337 | \r | |
338 | The TSC counting frequency is determined by comparing how far it counts\r | |
a012df5e SZ |
339 | during a 101.4 us period as determined by the ACPI timer.\r |
340 | The ACPI timer is used because it counts at a known frequency.\r | |
341 | The TSC is sampled, followed by waiting 363 counts of the ACPI timer,\r | |
342 | or 101.4 us. The TSC is then sampled again. The difference multiplied by\r | |
343 | 9861 is the TSC frequency. There will be a small error because of the\r | |
344 | overhead of reading the ACPI timer. An attempt is made to determine and\r | |
345 | compensate for this error.\r | |
62b8b5be SZ |
346 | \r |
347 | @return The number of TSC counts per second.\r | |
348 | \r | |
349 | **/\r | |
350 | UINT64\r | |
351 | InternalCalculateTscFrequency (\r | |
352 | VOID\r | |
353 | )\r | |
354 | {\r | |
355 | UINT64 StartTSC;\r | |
356 | UINT64 EndTSC;\r | |
357 | UINT16 TimerAddr;\r | |
358 | UINT32 Ticks;\r | |
359 | UINT64 TscFrequency;\r | |
360 | BOOLEAN InterruptState;\r | |
361 | \r | |
362 | InterruptState = SaveAndDisableInterrupts ();\r | |
363 | \r | |
364 | TimerAddr = InternalAcpiGetAcpiTimerIoPort ();\r | |
a012df5e SZ |
365 | //\r |
366 | // Compute the number of ticks to wait to measure TSC frequency.\r | |
367 | // Use 363 * 9861 = 3579543 Hz which is within 2 Hz of ACPI_TIMER_FREQUENCY.\r | |
368 | // 363 counts is a calibration time of 101.4 uS.\r | |
369 | //\r | |
b3b58d4d | 370 | Ticks = IoBitFieldRead32 (TimerAddr, 0, 23) + 363;\r |
62b8b5be SZ |
371 | \r |
372 | StartTSC = AsmReadTsc (); // Get base value for the TSC\r | |
373 | //\r | |
a012df5e | 374 | // Wait until the ACPI timer has counted 101.4 us.\r |
62b8b5be | 375 | // Timer wrap-arounds are handled correctly by this function.\r |
a012df5e SZ |
376 | // When the current ACPI timer value is greater than 'Ticks',\r |
377 | // the while loop will exit.\r | |
62b8b5be | 378 | //\r |
b3b58d4d | 379 | while (((Ticks - IoBitFieldRead32 (TimerAddr, 0, 23)) & BIT23) == 0) {\r |
62b8b5be SZ |
380 | CpuPause();\r |
381 | }\r | |
a012df5e | 382 | EndTSC = AsmReadTsc (); // TSC value 101.4 us later\r |
62b8b5be SZ |
383 | \r |
384 | TscFrequency = MultU64x32 (\r | |
a012df5e SZ |
385 | (EndTSC - StartTSC), // Number of TSC counts in 101.4 us\r |
386 | 9861 // Number of 101.4 us in a second\r | |
62b8b5be SZ |
387 | );\r |
388 | \r | |
389 | SetInterruptState (InterruptState);\r | |
390 | \r | |
391 | return TscFrequency;\r | |
392 | }\r | |
393 | \r |