]>
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.
17 #include <Library/DebugLib.h>
18 #include <Library/BaseLib.h>
19 #include <IndustryStandard/Acpi.h>
21 #include "AcpiTimerLib.h"
24 // The ACPI Time is a 24-bit counter
26 #define ACPI_TIMER_COUNT_SIZE BIT24
29 Stalls the CPU for at least the given number of ticks.
31 Stalls the CPU for at least the given number of ticks. It's invoked by
32 MicroSecondDelay() and NanoSecondDelay().
34 @param Delay A period of time to delay in ticks.
49 // The target timer count is calculated here
51 Ticks
= InternalAcpiGetTimerTick () + Delay
;
54 // Wait until time out
55 // Delay >= 2^23 could not be handled by this function
56 // Timer wrap-arounds are handled correctly by this function
58 while (((Ticks
- InternalAcpiGetTimerTick ()) & BIT23
) == 0) {
61 } while (Times
-- > 0);
65 Stalls the CPU for at least the given number of microseconds.
67 Stalls the CPU for the number of microseconds specified by MicroSeconds.
69 @param MicroSeconds The minimum number of microseconds to delay.
93 Stalls the CPU for at least the given number of nanoseconds.
95 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
97 @param NanoSeconds The minimum number of nanoseconds to delay.
121 Retrieves the current value of a 64-bit free running performance counter.
123 Retrieves the current value of a 64-bit free running performance counter. The
124 counter can either count up by 1 or count down by 1. If the physical
125 performance counter counts by a larger increment, then the counter values
126 must be translated. The properties of the counter can be retrieved from
127 GetPerformanceCounterProperties().
129 @return The current value of the free running performance counter.
134 GetPerformanceCounter (
138 return (UINT64
)InternalAcpiGetTimerTick ();
142 Retrieves the 64-bit frequency in Hz and the range of performance counter
145 If StartValue is not NULL, then the value that the performance counter starts
146 with immediately after is it rolls over is returned in StartValue. If
147 EndValue is not NULL, then the value that the performance counter end with
148 immediately before it rolls over is returned in EndValue. The 64-bit
149 frequency of the performance counter in Hz is always returned. If StartValue
150 is less than EndValue, then the performance counter counts up. If StartValue
151 is greater than EndValue, then the performance counter counts down. For
152 example, a 64-bit free running counter that counts up would have a StartValue
153 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
154 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
156 @param StartValue The value the performance counter starts with when it
158 @param EndValue The value that the performance counter ends with before
161 @return The frequency in Hz.
166 GetPerformanceCounterProperties (
167 OUT UINT64
*StartValue
, OPTIONAL
168 OUT UINT64
*EndValue OPTIONAL
171 if (StartValue
!= NULL
) {
175 if (EndValue
!= NULL
) {
176 *EndValue
= ACPI_TIMER_COUNT_SIZE
- 1;
179 return ACPI_TIMER_FREQUENCY
;
183 Converts elapsed ticks of performance counter to time in nanoseconds.
185 This function converts the elapsed ticks of running performance counter to
186 time value in unit of nanoseconds.
188 @param Ticks The number of elapsed ticks of running performance counter.
190 @return The elapsed time in nanoseconds.
195 GetTimeInNanoSecond (
204 // Time = --------- x 1,000,000,000
207 NanoSeconds
= MultU64x32 (DivU64x32Remainder (Ticks
, ACPI_TIMER_FREQUENCY
, &Remainder
), 1000000000u);
210 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
211 // will not overflow 64-bit.
213 NanoSeconds
+= DivU64x32 (MultU64x32 ((UINT64
) Remainder
, 1000000000u), ACPI_TIMER_FREQUENCY
);