2 ICH9 ACPI Timer implements one instance of Timer Library.
4 Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available under
7 the terms and conditions of the BSD License that accompanies this distribution.
8 The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "CommonHeader.h"
20 The constructor function enables ACPI IO space.
22 If ACPI I/O space not enabled, this function will enable it.
23 It will always return RETURN_SUCCESS.
25 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
30 IntelPchAcpiTimerLibConstructor (
34 if ((PchLpcPciCfg8(R_PCH_LPC_ACPI_BASE
) & B_PCH_LPC_ACPI_BASE_EN
) == 0) {
36 // If ACPI I/O space is not enabled, program ACPI I/O base address and enable it.
41 DEFAULT_PCI_BUS_NUMBER_PCH
,
42 PCI_DEVICE_NUMBER_PCH_LPC
,
43 PCI_FUNCTION_NUMBER_PCH_LPC
,
46 ((PcdGet16 (PcdPchAcpiIoPortBaseAddress
) & B_PCH_LPC_ACPI_BASE_BAR
) | B_PCH_LPC_ACPI_BASE_EN
)
49 return RETURN_SUCCESS
;
53 Internal function to read the current tick counter of ACPI.
55 Internal function to read the current tick counter of ACPI.
57 @return The tick counter read.
62 InternalAcpiGetTimerTick (
66 return IoRead32 (PcdGet16 (PcdPchAcpiIoPortBaseAddress
) + R_PCH_ACPI_PM1_TMR
);
70 Stalls the CPU for at least the given number of ticks.
72 Stalls the CPU for at least the given number of ticks. It's invoked by
73 MicroSecondDelay() and NanoSecondDelay().
75 @param Delay A period of time to delay in ticks.
91 // The target timer count is calculated here
93 Ticks
= InternalAcpiGetTimerTick () + Delay
;
96 // Wait until time out
97 // Delay >= 2^23 could not be handled by this function
98 // Timer wrap-arounds are handled correctly by this function
100 while (((Ticks
- InternalAcpiGetTimerTick ()) & BIT23
) == 0) {
103 } while (Times
-- > 0);
107 Stalls the CPU for at least the given number of microseconds.
109 Stalls the CPU for the number of microseconds specified by MicroSeconds.
111 @param MicroSeconds The minimum number of microseconds to delay.
119 IN UINTN MicroSeconds
126 V_PCH_ACPI_PM1_TMR_FREQUENCY
135 Stalls the CPU for at least the given number of nanoseconds.
137 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
139 @param NanoSeconds The minimum number of nanoseconds to delay.
154 V_PCH_ACPI_PM1_TMR_FREQUENCY
163 Retrieves the current value of a 64-bit free running performance counter.
165 Retrieves the current value of a 64-bit free running performance counter. The
166 counter can either count up by 1 or count down by 1. If the physical
167 performance counter counts by a larger increment, then the counter values
168 must be translated. The properties of the counter can be retrieved from
169 GetPerformanceCounterProperties().
171 @return The current value of the free running performance counter.
176 GetPerformanceCounter (
180 return (UINT64
)InternalAcpiGetTimerTick ();
184 Retrieves the 64-bit frequency in Hz and the range of performance counter
187 If StartValue is not NULL, then the value that the performance counter starts
188 with immediately after is it rolls over is returned in StartValue. If
189 EndValue is not NULL, then the value that the performance counter end with
190 immediately before it rolls over is returned in EndValue. The 64-bit
191 frequency of the performance counter in Hz is always returned. If StartValue
192 is less than EndValue, then the performance counter counts up. If StartValue
193 is greater than EndValue, then the performance counter counts down. For
194 example, a 64-bit free running counter that counts up would have a StartValue
195 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
196 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
198 @param StartValue The value the performance counter starts with when it
200 @param EndValue The value that the performance counter ends with before
203 @return The frequency in Hz.
208 GetPerformanceCounterProperties (
209 OUT UINT64
*StartValue
, OPTIONAL
210 OUT UINT64
*EndValue OPTIONAL
213 if (StartValue
!= NULL
) {
217 if (EndValue
!= NULL
) {
218 *EndValue
= V_PCH_ACPI_PM1_TMR_MAX_VAL
- 1;
221 return V_PCH_ACPI_PM1_TMR_FREQUENCY
;
225 Converts elapsed ticks of performance counter to time in nanoseconds.
227 This function converts the elapsed ticks of running performance counter to
228 time value in unit of nanoseconds.
230 @param Ticks The number of elapsed ticks of running performance counter.
232 @return The elapsed time in nanoseconds.
237 GetTimeInNanoSecond (
246 // Time = --------- x 1,000,000,000
249 NanoSeconds
= MultU64x32 (DivU64x32Remainder (Ticks
, V_PCH_ACPI_PM1_TMR_FREQUENCY
, &Remainder
), 1000000000u);
252 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
253 // will not overflow 64-bit.
255 NanoSeconds
+= DivU64x32 (MultU64x32 ((UINT64
) Remainder
, 1000000000u), V_PCH_ACPI_PM1_TMR_FREQUENCY
);