]>
git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.c
2 ACPI Timer implements one instance of Timer Library.
4 Copyright (c) 2008 - 2012, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2011, Andrei Warkentin <andreiw@motorola.com>
7 This program and the accompanying materials are
8 licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Library/TimerLib.h>
19 #include <Library/BaseLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/PciLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/PcdLib.h>
24 #include <IndustryStandard/Pci22.h>
25 #include <IndustryStandard/Acpi.h>
28 // PCI Location of PIIX4 Power Management PCI Configuration Registers
30 #define PIIX4_POWER_MANAGEMENT_BUS 0x00
31 #define PIIX4_POWER_MANAGEMENT_DEVICE 0x01
32 #define PIIX4_POWER_MANAGEMENT_FUNCTION 0x03
35 // Macro to access PIIX4 Power Management PCI Configuration Registers
37 #define PIIX4_PCI_POWER_MANAGEMENT_REGISTER(Register) \
39 PIIX4_POWER_MANAGEMENT_BUS, \
40 PIIX4_POWER_MANAGEMENT_DEVICE, \
41 PIIX4_POWER_MANAGEMENT_FUNCTION, \
46 // PIIX4 Power Management PCI Configuration Registers
48 #define PMBA PIIX4_PCI_POWER_MANAGEMENT_REGISTER (0x40)
50 #define PMREGMISC PIIX4_PCI_POWER_MANAGEMENT_REGISTER (0x80)
54 // The ACPI Time in the PIIX4 is a 24-bit counter
56 #define ACPI_TIMER_COUNT_SIZE BIT24
59 // Offset in the PIIX4 Power Management Base Address to the ACPI Timer
61 #define ACPI_TIMER_OFFSET 0x8
64 The constructor function enables ACPI IO space.
66 If ACPI I/O space not enabled, this function will enable it.
67 It will always return RETURN_SUCCESS.
69 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
74 AcpiTimerLibConstructor (
79 // Check to see if the PIIX4 Power Management Base Address is already enabled
81 if ((PciRead8 (PMREGMISC
) & PMIOSE
) == 0) {
83 // If the PIIX4 Power Management Base Address is not programmed,
84 // then program the PIIX4 Power Management Base Address from a PCD.
86 PciAndThenOr32 (PMBA
, (UINT32
)(~0x0000FFC0), PcdGet16 (PcdAcpiPmBaseAddress
));
89 // Enable PMBA I/O port decodes in PMREGMISC
91 PciOr8 (PMREGMISC
, PMIOSE
);
94 return RETURN_SUCCESS
;
98 Internal function to read the current tick counter of ACPI.
100 Internal function to read the current tick counter of ACPI.
102 @return The tick counter read.
106 InternalAcpiGetTimerTick (
111 // Read PMBA to read and return the current ACPI timer value.
113 return IoRead32 ((PciRead32 (PMBA
) & ~PMBA_RTE
) + ACPI_TIMER_OFFSET
);
117 Stalls the CPU for at least the given number of ticks.
119 Stalls the CPU for at least the given number of ticks. It's invoked by
120 MicroSecondDelay() and NanoSecondDelay().
122 @param Delay A period of time to delay in ticks.
137 // The target timer count is calculated here
139 Ticks
= InternalAcpiGetTimerTick () + Delay
;
142 // Wait until time out
143 // Delay >= 2^23 could not be handled by this function
144 // Timer wrap-arounds are handled correctly by this function
146 while (((Ticks
- InternalAcpiGetTimerTick ()) & BIT23
) == 0) {
149 } while (Times
-- > 0);
153 Stalls the CPU for at least the given number of microseconds.
155 Stalls the CPU for the number of microseconds specified by MicroSeconds.
157 @param MicroSeconds The minimum number of microseconds to delay.
165 IN UINTN MicroSeconds
181 Stalls the CPU for at least the given number of nanoseconds.
183 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
185 @param NanoSeconds The minimum number of nanoseconds to delay.
209 Retrieves the current value of a 64-bit free running performance counter.
211 Retrieves the current value of a 64-bit free running performance counter. The
212 counter can either count up by 1 or count down by 1. If the physical
213 performance counter counts by a larger increment, then the counter values
214 must be translated. The properties of the counter can be retrieved from
215 GetPerformanceCounterProperties().
217 @return The current value of the free running performance counter.
222 GetPerformanceCounter (
226 return (UINT64
)InternalAcpiGetTimerTick ();
230 Retrieves the 64-bit frequency in Hz and the range of performance counter
233 If StartValue is not NULL, then the value that the performance counter starts
234 with immediately after is it rolls over is returned in StartValue. If
235 EndValue is not NULL, then the value that the performance counter end with
236 immediately before it rolls over is returned in EndValue. The 64-bit
237 frequency of the performance counter in Hz is always returned. If StartValue
238 is less than EndValue, then the performance counter counts up. If StartValue
239 is greater than EndValue, then the performance counter counts down. For
240 example, a 64-bit free running counter that counts up would have a StartValue
241 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
242 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
244 @param StartValue The value the performance counter starts with when it
246 @param EndValue The value that the performance counter ends with before
249 @return The frequency in Hz.
254 GetPerformanceCounterProperties (
255 OUT UINT64
*StartValue
, OPTIONAL
256 OUT UINT64
*EndValue OPTIONAL
259 if (StartValue
!= NULL
) {
263 if (EndValue
!= NULL
) {
264 *EndValue
= ACPI_TIMER_COUNT_SIZE
- 1;
267 return ACPI_TIMER_FREQUENCY
;
271 Converts elapsed ticks of performance counter to time in nanoseconds.
273 This function converts the elapsed ticks of running performance counter to
274 time value in unit of nanoseconds.
276 @param Ticks The number of elapsed ticks of running performance counter.
278 @return The elapsed time in nanoseconds.
283 GetTimeInNanoSecond (
292 // Time = --------- x 1,000,000,000
295 NanoSeconds
= MultU64x32 (DivU64x32Remainder (Ticks
, ACPI_TIMER_FREQUENCY
, &Remainder
), 1000000000u);
298 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
299 // will not overflow 64-bit.
301 NanoSeconds
+= DivU64x32 (MultU64x32 ((UINT64
) Remainder
, 1000000000u), ACPI_TIMER_FREQUENCY
);