]>
git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.c
027912e0f7565d393df6411fea219c2944e54256
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>
23 #include <Library/PcdLib.h>
26 // PIIX4 Power Management Base Address
30 #define PCI_BAR_IO 0x1
31 #define ACPI_TIMER_FREQUENCY 3579545
32 #define ACPI_TIMER_COUNT_SIZE 0x01000000
33 #define ACPI_TIMER_OFFSET 0x8
36 The constructor function enables ACPI IO space.
38 If ACPI I/O space not enabled, this function will enable it.
39 It will always return RETURN_SUCCESS.
41 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
46 AcpiTimerLibConstructor (
55 if (PciRead8 (PCI_LIB_ADDRESS (0,Device
,3,0x80)) & 1) {
56 mPmba
= PciRead32 (PCI_LIB_ADDRESS (0,Device
,3,0x40));
57 ASSERT (mPmba
& PCI_BAR_IO
);
60 mPmba
= PcdGet16 (PcdAcpiPmBaseAddress
);
62 PciAndThenOr32 (PCI_LIB_ADDRESS (0,Device
,3,0x40),
63 (UINT32
) ~0xfc0, mPmba
);
64 PciOr8 (PCI_LIB_ADDRESS (0,Device
,3,0x04), 0x01);
68 // ACPI Timer enable is in Bus 0, Device ?, Function 3
70 PciOr8 (PCI_LIB_ADDRESS (0,Device
,3,0x80), 0x01);
71 return RETURN_SUCCESS
;
75 Internal function to read the current tick counter of ACPI.
77 Internal function to read the current tick counter of ACPI.
79 @return The tick counter read.
84 InternalAcpiGetTimerTick (
88 return IoRead32 (mPmba
+ ACPI_TIMER_OFFSET
);
92 Stalls the CPU for at least the given number of ticks.
94 Stalls the CPU for at least the given number of ticks. It's invoked by
95 MicroSecondDelay() and NanoSecondDelay().
97 @param Delay A period of time to delay in ticks.
113 // The target timer count is calculated here
115 Ticks
= InternalAcpiGetTimerTick () + Delay
;
118 // Wait until time out
119 // Delay >= 2^23 could not be handled by this function
120 // Timer wrap-arounds are handled correctly by this function
122 while (((Ticks
- InternalAcpiGetTimerTick ()) & BIT23
) == 0) {
125 } while (Times
-- > 0);
129 Stalls the CPU for at least the given number of microseconds.
131 Stalls the CPU for the number of microseconds specified by MicroSeconds.
133 @param MicroSeconds The minimum number of microseconds to delay.
141 IN UINTN MicroSeconds
157 Stalls the CPU for at least the given number of nanoseconds.
159 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
161 @param NanoSeconds The minimum number of nanoseconds to delay.
185 Retrieves the current value of a 64-bit free running performance counter.
187 Retrieves the current value of a 64-bit free running performance counter. The
188 counter can either count up by 1 or count down by 1. If the physical
189 performance counter counts by a larger increment, then the counter values
190 must be translated. The properties of the counter can be retrieved from
191 GetPerformanceCounterProperties().
193 @return The current value of the free running performance counter.
198 GetPerformanceCounter (
202 return (UINT64
)InternalAcpiGetTimerTick ();
206 Retrieves the 64-bit frequency in Hz and the range of performance counter
209 If StartValue is not NULL, then the value that the performance counter starts
210 with immediately after is it rolls over is returned in StartValue. If
211 EndValue is not NULL, then the value that the performance counter end with
212 immediately before it rolls over is returned in EndValue. The 64-bit
213 frequency of the performance counter in Hz is always returned. If StartValue
214 is less than EndValue, then the performance counter counts up. If StartValue
215 is greater than EndValue, then the performance counter counts down. For
216 example, a 64-bit free running counter that counts up would have a StartValue
217 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
218 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
220 @param StartValue The value the performance counter starts with when it
222 @param EndValue The value that the performance counter ends with before
225 @return The frequency in Hz.
230 GetPerformanceCounterProperties (
231 OUT UINT64
*StartValue
, OPTIONAL
232 OUT UINT64
*EndValue OPTIONAL
235 if (StartValue
!= NULL
) {
239 if (EndValue
!= NULL
) {
240 *EndValue
= ACPI_TIMER_COUNT_SIZE
- 1;
243 return ACPI_TIMER_FREQUENCY
;
247 Converts elapsed ticks of performance counter to time in nanoseconds.
249 This function converts the elapsed ticks of running performance counter to
250 time value in unit of nanoseconds.
252 @param Ticks The number of elapsed ticks of running performance counter.
254 @return The elapsed time in nanoseconds.
259 GetTimeInNanoSecond (
268 // Time = --------- x 1,000,000,000
271 NanoSeconds
= MultU64x32 (DivU64x32Remainder (Ticks
, ACPI_TIMER_FREQUENCY
, &Remainder
), 1000000000u);
274 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
275 // will not overflow 64-bit.
277 NanoSeconds
+= DivU64x32 (MultU64x32 ((UINT64
) Remainder
, 1000000000u), ACPI_TIMER_FREQUENCY
);