2 Timer Library functions built upon ACPI on IA32/x64.
4 ACPI power management timer is a 24-bit or 32-bit fixed rate free running count-up
5 timer that runs off a 3.579545 MHz clock.
6 When startup, Duet will check the FADT to determine whether the PM timer is a
7 32-bit or 25-bit timer.
9 Copyright (c) 2006 - 2007, Intel Corporation. All rights reserved.<BR>
10 This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include <Library/TimerLib.h>
22 #include <Library/BaseLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/HobLib.h>
25 #include <Guid/AcpiDescription.h>
26 #include <Library/IoLib.h>
27 #include <Library/PciLib.h>
29 EFI_ACPI_DESCRIPTION
*gAcpiDesc
= NULL
;
32 Internal function to get Acpi information from HOB.
34 @return Pointer to ACPI description structure.
37 InternalGetApciDescrptionTable (
41 EFI_PEI_HOB_POINTERS GuidHob
;
43 if (gAcpiDesc
!= NULL
) {
47 GuidHob
.Raw
= GetFirstGuidHob (&gEfiAcpiDescriptionGuid
);
48 if (GuidHob
.Raw
!= NULL
) {
49 gAcpiDesc
= GET_GUID_HOB_DATA (GuidHob
.Guid
);
50 DEBUG ((EFI_D_INFO
, "ACPI Timer: PM_TMR_BLK.RegisterBitWidth = 0x%X\n", gAcpiDesc
->PM_TMR_BLK
.RegisterBitWidth
));
51 DEBUG ((EFI_D_INFO
, "ACPI Timer: PM_TMR_BLK.Address = 0x%X\n", gAcpiDesc
->PM_TMR_BLK
.Address
));
54 DEBUG ((EFI_D_ERROR
, "Fail to get Acpi description table from hob\n"));
60 Internal function to read the current tick counter of ACPI.
62 @return The tick counter read.
67 InternalAcpiGetTimerTick (
71 return IoRead32 ((UINTN
)gAcpiDesc
->PM_TMR_BLK
.Address
);
75 Stalls the CPU for at least the given number of ticks.
77 Stalls the CPU for at least the given number of ticks. It's invoked by
78 MicroSecondDelay() and NanoSecondDelay().
80 @param Delay A period of time to delay in ticks.
92 Times
= Delay
>> (gAcpiDesc
->PM_TMR_BLK
.RegisterBitWidth
- 2);
93 Delay
&= (1 << (gAcpiDesc
->PM_TMR_BLK
.RegisterBitWidth
- 2)) - 1;
96 // The target timer count is calculated here
98 Ticks
= InternalAcpiGetTimerTick () + Delay
;
99 Delay
= 1 << (gAcpiDesc
->PM_TMR_BLK
.RegisterBitWidth
- 2);
101 // Wait until time out
102 // Delay >= 2^23 (if ACPI provide 24-bit timer) or Delay >= 2^31 (if ACPI
103 // provide 32-bit timer) could not be handled by this function
104 // Timer wrap-arounds are handled correctly by this function
106 while (((Ticks
- InternalAcpiGetTimerTick ()) & (1 << (gAcpiDesc
->PM_TMR_BLK
.RegisterBitWidth
- 1))) == 0) {
109 } while (Times
-- > 0);
113 Stalls the CPU for at least the given number of microseconds.
115 Stalls the CPU for the number of microseconds specified by MicroSeconds.
117 @param MicroSeconds The minimum number of microseconds to delay.
125 IN UINTN MicroSeconds
129 if (InternalGetApciDescrptionTable() == NULL
) {
146 Stalls the CPU for at least the given number of nanoseconds.
148 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
150 @param NanoSeconds The minimum number of nanoseconds to delay.
161 if (InternalGetApciDescrptionTable() == NULL
) {
178 Retrieves the current value of a 64-bit free running performance counter.
180 Retrieves the current value of a 64-bit free running performance counter. The
181 counter can either count up by 1 or count down by 1. If the physical
182 performance counter counts by a larger increment, then the counter values
183 must be translated. The properties of the counter can be retrieved from
184 GetPerformanceCounterProperties().
186 @return The current value of the free running performance counter.
191 GetPerformanceCounter (
195 if (InternalGetApciDescrptionTable() == NULL
) {
199 return (UINT64
)InternalAcpiGetTimerTick ();
203 Retrieves the 64-bit frequency in Hz and the range of performance counter
206 If StartValue is not NULL, then the value that the performance counter starts
207 with immediately after is it rolls over is returned in StartValue. If
208 EndValue is not NULL, then the value that the performance counter end with
209 immediately before it rolls over is returned in EndValue. The 64-bit
210 frequency of the performance counter in Hz is always returned. If StartValue
211 is less than EndValue, then the performance counter counts up. If StartValue
212 is greater than EndValue, then the performance counter counts down. For
213 example, a 64-bit free running counter that counts up would have a StartValue
214 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
215 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
217 @param StartValue The value the performance counter starts with when it
219 @param EndValue The value that the performance counter ends with before
222 @return The frequency in Hz.
227 GetPerformanceCounterProperties (
228 OUT UINT64
*StartValue
, OPTIONAL
229 OUT UINT64
*EndValue OPTIONAL
232 if (InternalGetApciDescrptionTable() == NULL
) {
236 if (StartValue
!= NULL
) {
240 if (EndValue
!= NULL
) {
241 *EndValue
= (1 << gAcpiDesc
->PM_TMR_BLK
.RegisterBitWidth
) - 1;