2 ACPI Timer implements one instance of Timer Library.
4 Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/TimerLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/PcdLib.h>
19 #include <Library/PciLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/DebugLib.h>
22 #include <IndustryStandard/Acpi.h>
25 Internal function to retrieves the 64-bit frequency in Hz.
27 Internal function to retrieves the 64-bit frequency in Hz.
29 @return The frequency in Hz.
33 InternalGetPerformanceCounterFrequency (
38 The constructor function enables ACPI IO space.
40 If ACPI I/O space not enabled, this function will enable it.
41 It will always return RETURN_SUCCESS.
43 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
48 AcpiTimerLibConstructor (
59 // ASSERT for the invalid PCD values. They must be configured to the real value.
61 ASSERT (PcdGet16 (PcdAcpiIoPciBarRegisterOffset
) != 0xFFFF);
62 ASSERT (PcdGet16 (PcdAcpiIoPortBaseAddress
) != 0xFFFF);
65 // If the register offset to the BAR for the ACPI I/O Port Base Address is 0x0000, then
66 // no PCI register programming is required to enable access to the the ACPI registers
67 // specified by PcdAcpiIoPortBaseAddress
69 if (PcdGet16 (PcdAcpiIoPciBarRegisterOffset
) == 0x0000) {
70 return RETURN_SUCCESS
;
74 // ASSERT for the invalid PCD values. They must be configured to the real value.
76 ASSERT (PcdGet8 (PcdAcpiIoPciDeviceNumber
) != 0xFF);
77 ASSERT (PcdGet8 (PcdAcpiIoPciFunctionNumber
) != 0xFF);
78 ASSERT (PcdGet16 (PcdAcpiIoPciEnableRegisterOffset
) != 0xFFFF);
81 // Retrieve the PCD values for the PCI configuration space required to program the ACPI I/O Port Base Address
83 Bus
= PcdGet8 (PcdAcpiIoPciBusNumber
);
84 Device
= PcdGet8 (PcdAcpiIoPciDeviceNumber
);
85 Function
= PcdGet8 (PcdAcpiIoPciFunctionNumber
);
86 EnableRegister
= PcdGet16 (PcdAcpiIoPciEnableRegisterOffset
);
87 EnableMask
= PcdGet8 (PcdAcpiIoBarEnableMask
);
90 // If ACPI I/O space is not enabled yet, program ACPI I/O base address and enable it.
92 if ((PciRead8 (PCI_LIB_ADDRESS (Bus
, Device
, Function
, EnableRegister
)) & EnableMask
) != EnableMask
) {
94 PCI_LIB_ADDRESS (Bus
, Device
, Function
, PcdGet16 (PcdAcpiIoPciBarRegisterOffset
)),
95 PcdGet16 (PcdAcpiIoPortBaseAddress
)
98 PCI_LIB_ADDRESS (Bus
, Device
, Function
, EnableRegister
),
103 return RETURN_SUCCESS
;
107 Internal function to retrieve the ACPI I/O Port Base Address.
109 Internal function to retrieve the ACPI I/O Port Base Address.
111 @return The 16-bit ACPI I/O Port Base Address.
115 InternalAcpiGetAcpiTimerIoPort (
121 Port
= PcdGet16 (PcdAcpiIoPortBaseAddress
);
124 // If the register offset to the BAR for the ACPI I/O Port Base Address is not 0x0000, then
125 // read the PCI register for the ACPI BAR value in case the BAR has been programmed to a
126 // value other than PcdAcpiIoPortBaseAddress
128 if (PcdGet16 (PcdAcpiIoPciBarRegisterOffset
) != 0x0000) {
129 Port
= PciRead16 (PCI_LIB_ADDRESS (
130 PcdGet8 (PcdAcpiIoPciBusNumber
),
131 PcdGet8 (PcdAcpiIoPciDeviceNumber
),
132 PcdGet8 (PcdAcpiIoPciFunctionNumber
),
133 PcdGet16 (PcdAcpiIoPciBarRegisterOffset
)
137 return (Port
& PcdGet16 (PcdAcpiIoPortBaseAddressMask
)) + PcdGet16 (PcdAcpiPm1TmrOffset
);
141 Stalls the CPU for at least the given number of ticks.
143 Stalls the CPU for at least the given number of ticks. It's invoked by
144 MicroSecondDelay() and NanoSecondDelay().
146 @param Delay A period of time to delay in ticks.
158 Port
= InternalAcpiGetAcpiTimerIoPort ();
163 // The target timer count is calculated here
165 Ticks
= IoBitFieldRead32 (Port
, 0, 23) + Delay
;
168 // Wait until time out
169 // Delay >= 2^23 could not be handled by this function
170 // Timer wrap-arounds are handled correctly by this function
172 while (((Ticks
- IoBitFieldRead32 (Port
, 0, 23)) & BIT23
) == 0) {
175 } while (Times
-- > 0);
179 Stalls the CPU for at least the given number of microseconds.
181 Stalls the CPU for the number of microseconds specified by MicroSeconds.
183 @param MicroSeconds The minimum number of microseconds to delay.
191 IN UINTN MicroSeconds
207 Stalls the CPU for at least the given number of nanoseconds.
209 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
211 @param NanoSeconds The minimum number of nanoseconds to delay.
235 Retrieves the current value of a 64-bit free running performance counter.
237 Retrieves the current value of a 64-bit free running performance counter. The
238 counter can either count up by 1 or count down by 1. If the physical
239 performance counter counts by a larger increment, then the counter values
240 must be translated. The properties of the counter can be retrieved from
241 GetPerformanceCounterProperties().
243 @return The current value of the free running performance counter.
248 GetPerformanceCounter (
252 return AsmReadTsc ();
256 Retrieves the 64-bit frequency in Hz and the range of performance counter
259 If StartValue is not NULL, then the value that the performance counter starts
260 with immediately after is it rolls over is returned in StartValue. If
261 EndValue is not NULL, then the value that the performance counter end with
262 immediately before it rolls over is returned in EndValue. The 64-bit
263 frequency of the performance counter in Hz is always returned. If StartValue
264 is less than EndValue, then the performance counter counts up. If StartValue
265 is greater than EndValue, then the performance counter counts down. For
266 example, a 64-bit free running counter that counts up would have a StartValue
267 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
268 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
270 @param StartValue The value the performance counter starts with when it
272 @param EndValue The value that the performance counter ends with before
275 @return The frequency in Hz.
280 GetPerformanceCounterProperties (
281 OUT UINT64
*StartValue
, OPTIONAL
282 OUT UINT64
*EndValue OPTIONAL
285 if (StartValue
!= NULL
) {
289 if (EndValue
!= NULL
) {
290 *EndValue
= 0xffffffffffffffffULL
;
292 return InternalGetPerformanceCounterFrequency ();
296 Converts elapsed ticks of performance counter to time in nanoseconds.
298 This function converts the elapsed ticks of running performance counter to
299 time value in unit of nanoseconds.
301 @param Ticks The number of elapsed ticks of running performance counter.
303 @return The elapsed time in nanoseconds.
308 GetTimeInNanoSecond (
317 Frequency
= GetPerformanceCounterProperties (NULL
, NULL
);
321 // Time = --------- x 1,000,000,000
324 NanoSeconds
= MultU64x32 (DivU64x64Remainder (Ticks
, Frequency
, &Remainder
), 1000000000u);
327 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
328 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
329 // i.e. highest bit set in Remainder should <= 33.
331 Shift
= MAX (0, HighBitSet64 (Remainder
) - 33);
332 Remainder
= RShiftU64 (Remainder
, (UINTN
) Shift
);
333 Frequency
= RShiftU64 (Frequency
, (UINTN
) Shift
);
334 NanoSeconds
+= DivU64x64Remainder (MultU64x32 (Remainder
, 1000000000u), Frequency
, NULL
);
340 Calculate TSC frequency.
342 The TSC counting frequency is determined by comparing how far it counts
343 during a 101.4 us period as determined by the ACPI timer.
344 The ACPI timer is used because it counts at a known frequency.
345 The TSC is sampled, followed by waiting 363 counts of the ACPI timer,
346 or 101.4 us. The TSC is then sampled again. The difference multiplied by
347 9861 is the TSC frequency. There will be a small error because of the
348 overhead of reading the ACPI timer. An attempt is made to determine and
349 compensate for this error.
351 @return The number of TSC counts per second.
355 InternalCalculateTscFrequency (
364 BOOLEAN InterruptState
;
366 InterruptState
= SaveAndDisableInterrupts ();
368 TimerAddr
= InternalAcpiGetAcpiTimerIoPort ();
370 // Compute the number of ticks to wait to measure TSC frequency.
371 // Use 363 * 9861 = 3579543 Hz which is within 2 Hz of ACPI_TIMER_FREQUENCY.
372 // 363 counts is a calibration time of 101.4 uS.
374 Ticks
= IoBitFieldRead32 (TimerAddr
, 0, 23) + 363;
376 StartTSC
= AsmReadTsc (); // Get base value for the TSC
378 // Wait until the ACPI timer has counted 101.4 us.
379 // Timer wrap-arounds are handled correctly by this function.
380 // When the current ACPI timer value is greater than 'Ticks',
381 // the while loop will exit.
383 while (((Ticks
- IoBitFieldRead32 (TimerAddr
, 0, 23)) & BIT23
) == 0) {
386 EndTSC
= AsmReadTsc (); // TSC value 101.4 us later
388 TscFrequency
= MultU64x32 (
389 (EndTSC
- StartTSC
), // Number of TSC counts in 101.4 us
390 9861 // Number of 101.4 us in a second
393 SetInterruptState (InterruptState
);