]>
git.proxmox.com Git - mirror_edk2.git/blob - UefiPayloadPkg/Library/AcpiTimerLib/AcpiTimerLib.c
2 ACPI Timer implements one instance of Timer Library.
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/TimerLib.h>
11 #include <Library/BaseLib.h>
12 #include <Library/IoLib.h>
13 #include <Library/HobLib.h>
14 #include <Library/DebugLib.h>
16 #include <Guid/AcpiBoardInfoGuid.h>
17 #include <IndustryStandard/Acpi.h>
19 #define ACPI_TIMER_COUNT_SIZE BIT24
21 UINTN mPmTimerReg
= 0;
24 The constructor function enables ACPI IO space.
26 If ACPI I/O space not enabled, this function will enable it.
27 It will always return RETURN_SUCCESS.
29 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
34 AcpiTimerLibConstructor (
38 EFI_HOB_GUID_TYPE
*GuidHob
;
39 ACPI_BOARD_INFO
*pAcpiBoardInfo
;
42 // Find the acpi board information guid hob
44 GuidHob
= GetFirstGuidHob (&gUefiAcpiBoardInfoGuid
);
45 ASSERT (GuidHob
!= NULL
);
47 pAcpiBoardInfo
= (ACPI_BOARD_INFO
*)GET_GUID_HOB_DATA (GuidHob
);
49 mPmTimerReg
= (UINTN
)pAcpiBoardInfo
->PmTimerRegBase
;
55 Internal function to read the current tick counter of ACPI.
57 Internal function to read the current tick counter of ACPI.
59 @return The tick counter read.
63 InternalAcpiGetTimerTick (
67 if (mPmTimerReg
== 0) {
68 AcpiTimerLibConstructor ();
70 return IoRead32 (mPmTimerReg
);
74 Stalls the CPU for at least the given number of ticks.
76 Stalls the CPU for at least the given number of ticks. It's invoked by
77 MicroSecondDelay() and NanoSecondDelay().
79 @param Delay A period of time to delay in ticks.
94 // The target timer count is calculated here
96 Ticks
= InternalAcpiGetTimerTick () + Delay
;
99 // Wait until time out
100 // Delay >= 2^23 could not be handled by this function
101 // Timer wrap-arounds are handled correctly by this function
103 while (((Ticks
- InternalAcpiGetTimerTick ()) & BIT23
) == 0) {
106 } while (Times
-- > 0);
110 Stalls the CPU for at least the given number of microseconds.
112 Stalls the CPU for the number of microseconds specified by MicroSeconds.
114 @param MicroSeconds The minimum number of microseconds to delay.
122 IN UINTN MicroSeconds
138 Stalls the CPU for at least the given number of nanoseconds.
140 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
142 @param NanoSeconds The minimum number of nanoseconds to delay.
166 Retrieves the current value of a 64-bit free running performance counter.
168 Retrieves the current value of a 64-bit free running performance counter. The
169 counter can either count up by 1 or count down by 1. If the physical
170 performance counter counts by a larger increment, then the counter values
171 must be translated. The properties of the counter can be retrieved from
172 GetPerformanceCounterProperties().
174 @return The current value of the free running performance counter.
179 GetPerformanceCounter (
183 return (UINT64
)InternalAcpiGetTimerTick ();
187 Retrieves the 64-bit frequency in Hz and the range of performance counter
190 If StartValue is not NULL, then the value that the performance counter starts
191 with immediately after is it rolls over is returned in StartValue. If
192 EndValue is not NULL, then the value that the performance counter end with
193 immediately before it rolls over is returned in EndValue. The 64-bit
194 frequency of the performance counter in Hz is always returned. If StartValue
195 is less than EndValue, then the performance counter counts up. If StartValue
196 is greater than EndValue, then the performance counter counts down. For
197 example, a 64-bit free running counter that counts up would have a StartValue
198 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
199 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
201 @param StartValue The value the performance counter starts with when it
203 @param EndValue The value that the performance counter ends with before
206 @return The frequency in Hz.
211 GetPerformanceCounterProperties (
212 OUT UINT64
*StartValue
, OPTIONAL
213 OUT UINT64
*EndValue OPTIONAL
216 if (StartValue
!= NULL
) {
220 if (EndValue
!= NULL
) {
221 *EndValue
= ACPI_TIMER_COUNT_SIZE
- 1;
224 return ACPI_TIMER_FREQUENCY
;
228 Converts elapsed ticks of performance counter to time in nanoseconds.
230 This function converts the elapsed ticks of running performance counter to
231 time value in unit of nanoseconds.
233 @param Ticks The number of elapsed ticks of running performance counter.
235 @return The elapsed time in nanoseconds.
240 GetTimeInNanoSecond (
249 Frequency
= GetPerformanceCounterProperties (NULL
, NULL
);
253 // Time = --------- x 1,000,000,000
256 NanoSeconds
= MultU64x32 (DivU64x64Remainder (Ticks
, Frequency
, &Remainder
), 1000000000u);
259 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
260 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
261 // i.e. highest bit set in Remainder should <= 33.
263 Shift
= MAX (0, HighBitSet64 (Remainder
) - 33);
264 Remainder
= RShiftU64 (Remainder
, (UINTN
) Shift
);
265 Frequency
= RShiftU64 (Frequency
, (UINTN
) Shift
);
266 NanoSeconds
+= DivU64x64Remainder (MultU64x32 (Remainder
, 1000000000u), Frequency
, NULL
);