]>
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, Intel Corporation<BR> All rights
5 reserved. This program and the accompanying materials are
6 licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/TimerLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/IoLib.h>
19 #include <Library/PciLib.h>
21 #define ACPI_TIMER_FREQUENCY 3579545
22 #define ACPI_TIMER_COUNT_SIZE 0x01000000
25 The constructor function enables ACPI IO space.
27 If ACPI I/O space not enabled, this function will enable it.
28 It will always return RETURN_SUCCESS.
30 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
35 AcpiTimerLibConstructor (
45 // ACPI Timer enable is in Bus 0, Device ?, Function 3
47 PciOr8 (PCI_LIB_ADDRESS (0,Device
,3,0x04), 0x01);
48 PciAndThenOr32 (PCI_LIB_ADDRESS (0,Device
,3,0x40), (UINT32
) ~0xfc0, 0x400);
49 PciOr8 (PCI_LIB_ADDRESS (0,Device
,3,0x80), 0x01); 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 (0x408);
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
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.
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
= ACPI_TIMER_COUNT_SIZE
- 1;
221 return ACPI_TIMER_FREQUENCY
;