2 ACPI Timer implements one instance of Timer Library.
4 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/TimerLib.h>
11 #include <Library/BaseLib.h>
12 #include <Library/PcdLib.h>
13 #include <Library/PciLib.h>
14 #include <Library/IoLib.h>
15 #include <Library/DebugLib.h>
16 #include <IndustryStandard/Acpi.h>
18 GUID mFrequencyHobGuid
= { 0x3fca54f6, 0xe1a2, 0x4b20, { 0xbe, 0x76, 0x92, 0x6b, 0x4b, 0x48, 0xbf, 0xaa }};
21 Internal function to retrieves the 64-bit frequency in Hz.
23 Internal function to retrieves the 64-bit frequency in Hz.
25 @return The frequency in Hz.
29 InternalGetPerformanceCounterFrequency (
34 The constructor function enables ACPI IO space.
36 If ACPI I/O space not enabled, this function will enable it.
37 It will always return RETURN_SUCCESS.
39 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
44 AcpiTimerLibConstructor (
55 // ASSERT for the invalid PCD values. They must be configured to the real value.
57 ASSERT (PcdGet16 (PcdAcpiIoPciBarRegisterOffset
) != 0xFFFF);
58 ASSERT (PcdGet16 (PcdAcpiIoPortBaseAddress
) != 0xFFFF);
61 // If the register offset to the BAR for the ACPI I/O Port Base Address is 0x0000, then
62 // no PCI register programming is required to enable access to the the ACPI registers
63 // specified by PcdAcpiIoPortBaseAddress
65 if (PcdGet16 (PcdAcpiIoPciBarRegisterOffset
) == 0x0000) {
66 return RETURN_SUCCESS
;
70 // ASSERT for the invalid PCD values. They must be configured to the real value.
72 ASSERT (PcdGet8 (PcdAcpiIoPciDeviceNumber
) != 0xFF);
73 ASSERT (PcdGet8 (PcdAcpiIoPciFunctionNumber
) != 0xFF);
74 ASSERT (PcdGet16 (PcdAcpiIoPciEnableRegisterOffset
) != 0xFFFF);
77 // Retrieve the PCD values for the PCI configuration space required to program the ACPI I/O Port Base Address
79 Bus
= PcdGet8 (PcdAcpiIoPciBusNumber
);
80 Device
= PcdGet8 (PcdAcpiIoPciDeviceNumber
);
81 Function
= PcdGet8 (PcdAcpiIoPciFunctionNumber
);
82 EnableRegister
= PcdGet16 (PcdAcpiIoPciEnableRegisterOffset
);
83 EnableMask
= PcdGet8 (PcdAcpiIoBarEnableMask
);
86 // If ACPI I/O space is not enabled yet, program ACPI I/O base address and enable it.
88 if ((PciRead8 (PCI_LIB_ADDRESS (Bus
, Device
, Function
, EnableRegister
)) & EnableMask
) != EnableMask
) {
90 PCI_LIB_ADDRESS (Bus
, Device
, Function
, PcdGet16 (PcdAcpiIoPciBarRegisterOffset
)),
91 PcdGet16 (PcdAcpiIoPortBaseAddress
)
94 PCI_LIB_ADDRESS (Bus
, Device
, Function
, EnableRegister
),
99 return RETURN_SUCCESS
;
103 Internal function to retrieve the ACPI I/O Port Base Address.
105 Internal function to retrieve the ACPI I/O Port Base Address.
107 @return The 16-bit ACPI I/O Port Base Address.
111 InternalAcpiGetAcpiTimerIoPort (
117 Port
= PcdGet16 (PcdAcpiIoPortBaseAddress
);
120 // If the register offset to the BAR for the ACPI I/O Port Base Address is not 0x0000, then
121 // read the PCI register for the ACPI BAR value in case the BAR has been programmed to a
122 // value other than PcdAcpiIoPortBaseAddress
124 if (PcdGet16 (PcdAcpiIoPciBarRegisterOffset
) != 0x0000) {
125 Port
= PciRead16 (PCI_LIB_ADDRESS (
126 PcdGet8 (PcdAcpiIoPciBusNumber
),
127 PcdGet8 (PcdAcpiIoPciDeviceNumber
),
128 PcdGet8 (PcdAcpiIoPciFunctionNumber
),
129 PcdGet16 (PcdAcpiIoPciBarRegisterOffset
)
133 return (Port
& PcdGet16 (PcdAcpiIoPortBaseAddressMask
)) + PcdGet16 (PcdAcpiPm1TmrOffset
);
137 Stalls the CPU for at least the given number of ticks.
139 Stalls the CPU for at least the given number of ticks. It's invoked by
140 MicroSecondDelay() and NanoSecondDelay().
142 @param Delay A period of time to delay in ticks.
154 Port
= InternalAcpiGetAcpiTimerIoPort ();
159 // The target timer count is calculated here
161 Ticks
= IoBitFieldRead32 (Port
, 0, 23) + Delay
;
164 // Wait until time out
165 // Delay >= 2^23 could not be handled by this function
166 // Timer wrap-arounds are handled correctly by this function
168 while (((Ticks
- IoBitFieldRead32 (Port
, 0, 23)) & BIT23
) == 0) {
171 } while (Times
-- > 0);
175 Stalls the CPU for at least the given number of microseconds.
177 Stalls the CPU for the number of microseconds specified by MicroSeconds.
179 @param MicroSeconds The minimum number of microseconds to delay.
187 IN UINTN MicroSeconds
203 Stalls the CPU for at least the given number of nanoseconds.
205 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
207 @param NanoSeconds The minimum number of nanoseconds to delay.
231 Retrieves the current value of a 64-bit free running performance counter.
233 Retrieves the current value of a 64-bit free running performance counter. The
234 counter can either count up by 1 or count down by 1. If the physical
235 performance counter counts by a larger increment, then the counter values
236 must be translated. The properties of the counter can be retrieved from
237 GetPerformanceCounterProperties().
239 @return The current value of the free running performance counter.
244 GetPerformanceCounter (
248 return AsmReadTsc ();
252 Retrieves the 64-bit frequency in Hz and the range of performance counter
255 If StartValue is not NULL, then the value that the performance counter starts
256 with immediately after is it rolls over is returned in StartValue. If
257 EndValue is not NULL, then the value that the performance counter end with
258 immediately before it rolls over is returned in EndValue. The 64-bit
259 frequency of the performance counter in Hz is always returned. If StartValue
260 is less than EndValue, then the performance counter counts up. If StartValue
261 is greater than EndValue, then the performance counter counts down. For
262 example, a 64-bit free running counter that counts up would have a StartValue
263 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
264 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
266 @param StartValue The value the performance counter starts with when it
268 @param EndValue The value that the performance counter ends with before
271 @return The frequency in Hz.
276 GetPerformanceCounterProperties (
277 OUT UINT64
*StartValue
, OPTIONAL
278 OUT UINT64
*EndValue OPTIONAL
281 if (StartValue
!= NULL
) {
285 if (EndValue
!= NULL
) {
286 *EndValue
= 0xffffffffffffffffULL
;
288 return InternalGetPerformanceCounterFrequency ();
292 Converts elapsed ticks of performance counter to time in nanoseconds.
294 This function converts the elapsed ticks of running performance counter to
295 time value in unit of nanoseconds.
297 @param Ticks The number of elapsed ticks of running performance counter.
299 @return The elapsed time in nanoseconds.
304 GetTimeInNanoSecond (
313 Frequency
= GetPerformanceCounterProperties (NULL
, NULL
);
317 // Time = --------- x 1,000,000,000
320 NanoSeconds
= MultU64x32 (DivU64x64Remainder (Ticks
, Frequency
, &Remainder
), 1000000000u);
323 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
324 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
325 // i.e. highest bit set in Remainder should <= 33.
327 Shift
= MAX (0, HighBitSet64 (Remainder
) - 33);
328 Remainder
= RShiftU64 (Remainder
, (UINTN
) Shift
);
329 Frequency
= RShiftU64 (Frequency
, (UINTN
) Shift
);
330 NanoSeconds
+= DivU64x64Remainder (MultU64x32 (Remainder
, 1000000000u), Frequency
, NULL
);
336 Calculate TSC frequency.
338 The TSC counting frequency is determined by comparing how far it counts
339 during a 101.4 us period as determined by the ACPI timer.
340 The ACPI timer is used because it counts at a known frequency.
341 The TSC is sampled, followed by waiting 363 counts of the ACPI timer,
342 or 101.4 us. The TSC is then sampled again. The difference multiplied by
343 9861 is the TSC frequency. There will be a small error because of the
344 overhead of reading the ACPI timer. An attempt is made to determine and
345 compensate for this error.
347 @return The number of TSC counts per second.
351 InternalCalculateTscFrequency (
360 BOOLEAN InterruptState
;
362 InterruptState
= SaveAndDisableInterrupts ();
364 TimerAddr
= InternalAcpiGetAcpiTimerIoPort ();
366 // Compute the number of ticks to wait to measure TSC frequency.
367 // Use 363 * 9861 = 3579543 Hz which is within 2 Hz of ACPI_TIMER_FREQUENCY.
368 // 363 counts is a calibration time of 101.4 uS.
370 Ticks
= IoBitFieldRead32 (TimerAddr
, 0, 23) + 363;
372 StartTSC
= AsmReadTsc (); // Get base value for the TSC
374 // Wait until the ACPI timer has counted 101.4 us.
375 // Timer wrap-arounds are handled correctly by this function.
376 // When the current ACPI timer value is greater than 'Ticks',
377 // the while loop will exit.
379 while (((Ticks
- IoBitFieldRead32 (TimerAddr
, 0, 23)) & BIT23
) == 0) {
382 EndTSC
= AsmReadTsc (); // TSC value 101.4 us later
384 TscFrequency
= MultU64x32 (
385 (EndTSC
- StartTSC
), // Number of TSC counts in 101.4 us
386 9861 // Number of 101.4 us in a second
389 SetInterruptState (InterruptState
);