]>
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 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/DebugLib.h>
12 #include <Library/BaseLib.h>
13 #include <IndustryStandard/Acpi.h>
15 #include "AcpiTimerLib.h"
18 // The ACPI Time is a 24-bit counter
20 #define ACPI_TIMER_COUNT_SIZE BIT24
23 Stalls the CPU for at least the given number of ticks.
25 Stalls the CPU for at least the given number of ticks. It's invoked by
26 MicroSecondDelay() and NanoSecondDelay().
28 @param Delay A period of time to delay in ticks.
43 // The target timer count is calculated here
45 Ticks
= InternalAcpiGetTimerTick () + Delay
;
48 // Wait until time out
49 // Delay >= 2^23 could not be handled by this function
50 // Timer wrap-arounds are handled correctly by this function
52 while (((Ticks
- InternalAcpiGetTimerTick ()) & BIT23
) == 0) {
55 } while (Times
-- > 0);
59 Stalls the CPU for at least the given number of microseconds.
61 Stalls the CPU for the number of microseconds specified by MicroSeconds.
63 @param MicroSeconds The minimum number of microseconds to delay.
87 Stalls the CPU for at least the given number of nanoseconds.
89 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
91 @param NanoSeconds The minimum number of nanoseconds to delay.
115 Retrieves the current value of a 64-bit free running performance counter.
117 Retrieves the current value of a 64-bit free running performance counter. The
118 counter can either count up by 1 or count down by 1. If the physical
119 performance counter counts by a larger increment, then the counter values
120 must be translated. The properties of the counter can be retrieved from
121 GetPerformanceCounterProperties().
123 @return The current value of the free running performance counter.
128 GetPerformanceCounter (
132 return (UINT64
)InternalAcpiGetTimerTick ();
136 Retrieves the 64-bit frequency in Hz and the range of performance counter
139 If StartValue is not NULL, then the value that the performance counter starts
140 with immediately after is it rolls over is returned in StartValue. If
141 EndValue is not NULL, then the value that the performance counter end with
142 immediately before it rolls over is returned in EndValue. The 64-bit
143 frequency of the performance counter in Hz is always returned. If StartValue
144 is less than EndValue, then the performance counter counts up. If StartValue
145 is greater than EndValue, then the performance counter counts down. For
146 example, a 64-bit free running counter that counts up would have a StartValue
147 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
148 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
150 @param StartValue The value the performance counter starts with when it
152 @param EndValue The value that the performance counter ends with before
155 @return The frequency in Hz.
160 GetPerformanceCounterProperties (
161 OUT UINT64
*StartValue OPTIONAL
,
162 OUT UINT64
*EndValue OPTIONAL
165 if (StartValue
!= NULL
) {
169 if (EndValue
!= NULL
) {
170 *EndValue
= ACPI_TIMER_COUNT_SIZE
- 1;
173 return ACPI_TIMER_FREQUENCY
;
177 Converts elapsed ticks of performance counter to time in nanoseconds.
179 This function converts the elapsed ticks of running performance counter to
180 time value in unit of nanoseconds.
182 @param Ticks The number of elapsed ticks of running performance counter.
184 @return The elapsed time in nanoseconds.
189 GetTimeInNanoSecond (
198 // Time = --------- x 1,000,000,000
201 NanoSeconds
= MultU64x32 (DivU64x32Remainder (Ticks
, ACPI_TIMER_FREQUENCY
, &Remainder
), 1000000000u);
204 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
205 // will not overflow 64-bit.
207 NanoSeconds
+= DivU64x32 (MultU64x32 ((UINT64
)Remainder
, 1000000000u), ACPI_TIMER_FREQUENCY
);