]>
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>
23 #include <Library/PcdLib.h>
24 #include <IndustryStandard/Pci22.h>
27 // PIIX4 Power Management Base Address
31 #define PCI_BAR_IO 0x1
32 #define ACPI_TIMER_FREQUENCY 3579545
33 #define ACPI_TIMER_COUNT_SIZE 0x01000000
34 #define ACPI_TIMER_OFFSET 0x8
37 The constructor function enables ACPI IO space.
39 If ACPI I/O space not enabled, this function will enable it.
40 It will always return RETURN_SUCCESS.
42 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
47 AcpiTimerLibConstructor (
56 if (PciRead8 (PCI_LIB_ADDRESS (0,Device
,3,0x80)) & 1) {
57 mPmba
= PciRead32 (PCI_LIB_ADDRESS (0,Device
,3,0x40));
58 ASSERT (mPmba
& PCI_BAR_IO
);
61 mPmba
= PcdGet16 (PcdAcpiPmBaseAddress
);
63 PciAndThenOr32 (PCI_LIB_ADDRESS (0,Device
,3,0x40),
64 (UINT32
) ~0xFFC0, mPmba
);
66 PCI_LIB_ADDRESS (0, Device
, 3, PCI_COMMAND_OFFSET
),
67 EFI_PCI_COMMAND_IO_SPACE
72 // ACPI Timer enable is in Bus 0, Device ?, Function 3
74 PciOr8 (PCI_LIB_ADDRESS (0,Device
,3,0x80), 0x01);
75 return RETURN_SUCCESS
;
79 Internal function to read the current tick counter of ACPI.
81 Internal function to read the current tick counter of ACPI.
83 @return The tick counter read.
88 InternalAcpiGetTimerTick (
92 return IoRead32 (mPmba
+ ACPI_TIMER_OFFSET
);
96 Stalls the CPU for at least the given number of ticks.
98 Stalls the CPU for at least the given number of ticks. It's invoked by
99 MicroSecondDelay() and NanoSecondDelay().
101 @param Delay A period of time to delay in ticks.
117 // The target timer count is calculated here
119 Ticks
= InternalAcpiGetTimerTick () + Delay
;
122 // Wait until time out
123 // Delay >= 2^23 could not be handled by this function
124 // Timer wrap-arounds are handled correctly by this function
126 while (((Ticks
- InternalAcpiGetTimerTick ()) & BIT23
) == 0) {
129 } while (Times
-- > 0);
133 Stalls the CPU for at least the given number of microseconds.
135 Stalls the CPU for the number of microseconds specified by MicroSeconds.
137 @param MicroSeconds The minimum number of microseconds to delay.
145 IN UINTN MicroSeconds
161 Stalls the CPU for at least the given number of nanoseconds.
163 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
165 @param NanoSeconds The minimum number of nanoseconds to delay.
189 Retrieves the current value of a 64-bit free running performance counter.
191 Retrieves the current value of a 64-bit free running performance counter. The
192 counter can either count up by 1 or count down by 1. If the physical
193 performance counter counts by a larger increment, then the counter values
194 must be translated. The properties of the counter can be retrieved from
195 GetPerformanceCounterProperties().
197 @return The current value of the free running performance counter.
202 GetPerformanceCounter (
206 return (UINT64
)InternalAcpiGetTimerTick ();
210 Retrieves the 64-bit frequency in Hz and the range of performance counter
213 If StartValue is not NULL, then the value that the performance counter starts
214 with immediately after is it rolls over is returned in StartValue. If
215 EndValue is not NULL, then the value that the performance counter end with
216 immediately before it rolls over is returned in EndValue. The 64-bit
217 frequency of the performance counter in Hz is always returned. If StartValue
218 is less than EndValue, then the performance counter counts up. If StartValue
219 is greater than EndValue, then the performance counter counts down. For
220 example, a 64-bit free running counter that counts up would have a StartValue
221 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
222 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
224 @param StartValue The value the performance counter starts with when it
226 @param EndValue The value that the performance counter ends with before
229 @return The frequency in Hz.
234 GetPerformanceCounterProperties (
235 OUT UINT64
*StartValue
, OPTIONAL
236 OUT UINT64
*EndValue OPTIONAL
239 if (StartValue
!= NULL
) {
243 if (EndValue
!= NULL
) {
244 *EndValue
= ACPI_TIMER_COUNT_SIZE
- 1;
247 return ACPI_TIMER_FREQUENCY
;
251 Converts elapsed ticks of performance counter to time in nanoseconds.
253 This function converts the elapsed ticks of running performance counter to
254 time value in unit of nanoseconds.
256 @param Ticks The number of elapsed ticks of running performance counter.
258 @return The elapsed time in nanoseconds.
263 GetTimeInNanoSecond (
272 // Time = --------- x 1,000,000,000
275 NanoSeconds
= MultU64x32 (DivU64x32Remainder (Ticks
, ACPI_TIMER_FREQUENCY
, &Remainder
), 1000000000u);
278 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
279 // will not overflow 64-bit.
281 NanoSeconds
+= DivU64x32 (MultU64x32 ((UINT64
) Remainder
, 1000000000u), ACPI_TIMER_FREQUENCY
);