]>
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 - 2011, 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>
25 // PIIX4 Power Management Base Address
29 #define PCI_BAR_IO 0x1
30 #define ACPI_TIMER_FREQUENCY 3579545
31 #define ACPI_TIMER_COUNT_SIZE 0x01000000
32 #define ACPI_TIMER_OFFSET 0x8
35 The constructor function enables ACPI IO space.
37 If ACPI I/O space not enabled, this function will enable it.
38 It will always return RETURN_SUCCESS.
40 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
45 AcpiTimerLibConstructor (
54 if (PciRead8 (PCI_LIB_ADDRESS (0,Device
,3,0x80)) & 1) {
55 mPmba
= PciRead32 (PCI_LIB_ADDRESS (0,Device
,3,0x40));
56 ASSERT (mPmba
& PCI_BAR_IO
);
59 PciAndThenOr32 (PCI_LIB_ADDRESS (0,Device
,3,0x40),
60 (UINT32
) ~0xfc0, mPmba
);
61 PciOr8 (PCI_LIB_ADDRESS (0,Device
,3,0x04), 0x01);
65 // ACPI Timer enable is in Bus 0, Device ?, Function 3
67 PciOr8 (PCI_LIB_ADDRESS (0,Device
,3,0x80), 0x01);
68 return RETURN_SUCCESS
;
72 Internal function to read the current tick counter of ACPI.
74 Internal function to read the current tick counter of ACPI.
76 @return The tick counter read.
81 InternalAcpiGetTimerTick (
85 return IoRead32 (mPmba
+ ACPI_TIMER_OFFSET
);
89 Stalls the CPU for at least the given number of ticks.
91 Stalls the CPU for at least the given number of ticks. It's invoked by
92 MicroSecondDelay() and NanoSecondDelay().
94 @param Delay A period of time to delay in ticks.
110 // The target timer count is calculated here
112 Ticks
= InternalAcpiGetTimerTick () + Delay
;
115 // Wait until time out
116 // Delay >= 2^23 could not be handled by this function
117 // Timer wrap-arounds are handled correctly by this function
119 while (((Ticks
- InternalAcpiGetTimerTick ()) & BIT23
) == 0) {
122 } while (Times
-- > 0);
126 Stalls the CPU for at least the given number of microseconds.
128 Stalls the CPU for the number of microseconds specified by MicroSeconds.
130 @param MicroSeconds The minimum number of microseconds to delay.
138 IN UINTN MicroSeconds
154 Stalls the CPU for at least the given number of nanoseconds.
156 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
158 @param NanoSeconds The minimum number of nanoseconds to delay.
182 Retrieves the current value of a 64-bit free running performance counter.
184 Retrieves the current value of a 64-bit free running performance counter. The
185 counter can either count up by 1 or count down by 1. If the physical
186 performance counter counts by a larger increment, then the counter values
187 must be translated. The properties of the counter can be retrieved from
188 GetPerformanceCounterProperties().
190 @return The current value of the free running performance counter.
195 GetPerformanceCounter (
199 return (UINT64
)InternalAcpiGetTimerTick ();
203 Retrieves the 64-bit frequency in Hz and the range of performance counter
206 If StartValue is not NULL, then the value that the performance counter starts
207 with immediately after is it rolls over is returned in StartValue. If
208 EndValue is not NULL, then the value that the performance counter end with
209 immediately before it rolls over is returned in EndValue. The 64-bit
210 frequency of the performance counter in Hz is always returned. If StartValue
211 is less than EndValue, then the performance counter counts up. If StartValue
212 is greater than EndValue, then the performance counter counts down. For
213 example, a 64-bit free running counter that counts up would have a StartValue
214 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
215 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
217 @param StartValue The value the performance counter starts with when it
219 @param EndValue The value that the performance counter ends with before
222 @return The frequency in Hz.
227 GetPerformanceCounterProperties (
228 OUT UINT64
*StartValue
, OPTIONAL
229 OUT UINT64
*EndValue OPTIONAL
232 if (StartValue
!= NULL
) {
236 if (EndValue
!= NULL
) {
237 *EndValue
= ACPI_TIMER_COUNT_SIZE
- 1;
240 return ACPI_TIMER_FREQUENCY
;
244 Converts elapsed ticks of performance counter to time in nanoseconds.
246 This function converts the elapsed ticks of running performance counter to
247 time value in unit of nanoseconds.
249 @param Ticks The number of elapsed ticks of running performance counter.
251 @return The elapsed time in nanoseconds.
256 GetTimeInNanoSecond (
265 // Time = --------- x 1,000,000,000
268 NanoSeconds
= MultU64x32 (DivU64x32Remainder (Ticks
, ACPI_TIMER_FREQUENCY
, &Remainder
), 1000000000u);
271 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
272 // will not overflow 64-bit.
274 NanoSeconds
+= DivU64x32 (MultU64x32 ((UINT64
) Remainder
, 1000000000u), ACPI_TIMER_FREQUENCY
);