2 ICH9 ACPI Timer implements one instance of Timer Library.
4 Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include "CommonHeader.h"
15 The constructor function enables ACPI IO space.
17 If ACPI I/O space not enabled, this function will enable it.
18 It will always return RETURN_SUCCESS.
20 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
25 IntelPchAcpiTimerLibConstructor (
29 if ((PchLpcPciCfg8(R_PCH_LPC_ACPI_BASE
) & B_PCH_LPC_ACPI_BASE_EN
) == 0) {
31 // If ACPI I/O space is not enabled, program ACPI I/O base address and enable it.
36 DEFAULT_PCI_BUS_NUMBER_PCH
,
37 PCI_DEVICE_NUMBER_PCH_LPC
,
38 PCI_FUNCTION_NUMBER_PCH_LPC
,
41 ((PcdGet16 (PcdPchAcpiIoPortBaseAddress
) & B_PCH_LPC_ACPI_BASE_BAR
) | B_PCH_LPC_ACPI_BASE_EN
)
44 return RETURN_SUCCESS
;
48 Internal function to read the current tick counter of ACPI.
50 Internal function to read the current tick counter of ACPI.
52 @return The tick counter read.
57 InternalAcpiGetTimerTick (
61 return IoRead32 (PcdGet16 (PcdPchAcpiIoPortBaseAddress
) + R_PCH_ACPI_PM1_TMR
);
65 Stalls the CPU for at least the given number of ticks.
67 Stalls the CPU for at least the given number of ticks. It's invoked by
68 MicroSecondDelay() and NanoSecondDelay().
70 @param Delay A period of time to delay in ticks.
86 // The target timer count is calculated here
88 Ticks
= InternalAcpiGetTimerTick () + Delay
;
91 // Wait until time out
92 // Delay >= 2^23 could not be handled by this function
93 // Timer wrap-arounds are handled correctly by this function
95 while (((Ticks
- InternalAcpiGetTimerTick ()) & BIT23
) == 0) {
98 } while (Times
-- > 0);
102 Stalls the CPU for at least the given number of microseconds.
104 Stalls the CPU for the number of microseconds specified by MicroSeconds.
106 @param MicroSeconds The minimum number of microseconds to delay.
114 IN UINTN MicroSeconds
121 V_PCH_ACPI_PM1_TMR_FREQUENCY
130 Stalls the CPU for at least the given number of nanoseconds.
132 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
134 @param NanoSeconds The minimum number of nanoseconds to delay.
149 V_PCH_ACPI_PM1_TMR_FREQUENCY
158 Retrieves the current value of a 64-bit free running performance counter.
160 Retrieves the current value of a 64-bit free running performance counter. The
161 counter can either count up by 1 or count down by 1. If the physical
162 performance counter counts by a larger increment, then the counter values
163 must be translated. The properties of the counter can be retrieved from
164 GetPerformanceCounterProperties().
166 @return The current value of the free running performance counter.
171 GetPerformanceCounter (
175 return (UINT64
)InternalAcpiGetTimerTick ();
179 Retrieves the 64-bit frequency in Hz and the range of performance counter
182 If StartValue is not NULL, then the value that the performance counter starts
183 with immediately after is it rolls over is returned in StartValue. If
184 EndValue is not NULL, then the value that the performance counter end with
185 immediately before it rolls over is returned in EndValue. The 64-bit
186 frequency of the performance counter in Hz is always returned. If StartValue
187 is less than EndValue, then the performance counter counts up. If StartValue
188 is greater than EndValue, then the performance counter counts down. For
189 example, a 64-bit free running counter that counts up would have a StartValue
190 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
191 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
193 @param StartValue The value the performance counter starts with when it
195 @param EndValue The value that the performance counter ends with before
198 @return The frequency in Hz.
203 GetPerformanceCounterProperties (
204 OUT UINT64
*StartValue
, OPTIONAL
205 OUT UINT64
*EndValue OPTIONAL
208 if (StartValue
!= NULL
) {
212 if (EndValue
!= NULL
) {
213 *EndValue
= V_PCH_ACPI_PM1_TMR_MAX_VAL
- 1;
216 return V_PCH_ACPI_PM1_TMR_FREQUENCY
;
220 Converts elapsed ticks of performance counter to time in nanoseconds.
222 This function converts the elapsed ticks of running performance counter to
223 time value in unit of nanoseconds.
225 @param Ticks The number of elapsed ticks of running performance counter.
227 @return The elapsed time in nanoseconds.
232 GetTimeInNanoSecond (
241 // Time = --------- x 1,000,000,000
244 NanoSeconds
= MultU64x32 (DivU64x32Remainder (Ticks
, V_PCH_ACPI_PM1_TMR_FREQUENCY
, &Remainder
), 1000000000u);
247 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
248 // will not overflow 64-bit.
250 NanoSeconds
+= DivU64x32 (MultU64x32 ((UINT64
) Remainder
, 1000000000u), V_PCH_ACPI_PM1_TMR_FREQUENCY
);