VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/UefiPayloadPkg/Library/AcpiTimerLib/AcpiTimerLib.c@ 99404

Last change on this file since 99404 was 99404, checked in by vboxsync, 22 months ago

Devices/EFI/FirmwareNew: Update to edk2-stable202302 and make it build, bugref:4643

  • Property svn:eol-style set to native
File size: 6.4 KB
Line 
1/** @file
2 ACPI Timer implements one instance of Timer Library.
3
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7**/
8
9#include <PiPei.h>
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>
15
16#include <Guid/AcpiBoardInfoGuid.h>
17#include <IndustryStandard/Acpi.h>
18
19#define ACPI_TIMER_COUNT_SIZE BIT24
20
21UINTN mPmTimerReg = 0;
22
23/**
24 The constructor function enables ACPI IO space.
25
26 If ACPI I/O space not enabled, this function will enable it.
27 It will always return RETURN_SUCCESS.
28
29 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
30
31**/
32RETURN_STATUS
33EFIAPI
34AcpiTimerLibConstructor (
35 VOID
36 )
37{
38 EFI_HOB_GUID_TYPE *GuidHob;
39 ACPI_BOARD_INFO *pAcpiBoardInfo;
40
41 //
42 // Find the acpi board information guid hob
43 //
44 GuidHob = GetFirstGuidHob (&gUefiAcpiBoardInfoGuid);
45 ASSERT (GuidHob != NULL);
46
47 pAcpiBoardInfo = (ACPI_BOARD_INFO *)GET_GUID_HOB_DATA (GuidHob);
48
49 mPmTimerReg = (UINTN)pAcpiBoardInfo->PmTimerRegBase;
50
51 return EFI_SUCCESS;
52}
53
54/**
55 Internal function to read the current tick counter of ACPI.
56
57 Internal function to read the current tick counter of ACPI.
58
59 @return The tick counter read.
60
61**/
62UINT32
63InternalAcpiGetTimerTick (
64 VOID
65 )
66{
67 if (mPmTimerReg == 0) {
68 AcpiTimerLibConstructor ();
69 }
70
71 return IoRead32 (mPmTimerReg);
72}
73
74/**
75 Stalls the CPU for at least the given number of ticks.
76
77 Stalls the CPU for at least the given number of ticks. It's invoked by
78 MicroSecondDelay() and NanoSecondDelay().
79
80 @param Delay A period of time to delay in ticks.
81
82**/
83VOID
84InternalAcpiDelay (
85 IN UINT32 Delay
86 )
87{
88 UINT32 Ticks;
89 UINT32 Times;
90
91 Times = Delay >> 22;
92 Delay &= BIT22 - 1;
93 do {
94 //
95 // The target timer count is calculated here
96 //
97 Ticks = InternalAcpiGetTimerTick () + Delay;
98 Delay = BIT22;
99 //
100 // Wait until time out
101 // Delay >= 2^23 could not be handled by this function
102 // Timer wrap-arounds are handled correctly by this function
103 //
104 while (((Ticks - InternalAcpiGetTimerTick ()) & BIT23) == 0) {
105 CpuPause ();
106 }
107 } while (Times-- > 0);
108}
109
110/**
111 Stalls the CPU for at least the given number of microseconds.
112
113 Stalls the CPU for the number of microseconds specified by MicroSeconds.
114
115 @param MicroSeconds The minimum number of microseconds to delay.
116
117 @return MicroSeconds
118
119**/
120UINTN
121EFIAPI
122MicroSecondDelay (
123 IN UINTN MicroSeconds
124 )
125{
126 InternalAcpiDelay (
127 (UINT32)DivU64x32 (
128 MultU64x32 (
129 MicroSeconds,
130 ACPI_TIMER_FREQUENCY
131 ),
132 1000000u
133 )
134 );
135 return MicroSeconds;
136}
137
138/**
139 Stalls the CPU for at least the given number of nanoseconds.
140
141 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
142
143 @param NanoSeconds The minimum number of nanoseconds to delay.
144
145 @return NanoSeconds
146
147**/
148UINTN
149EFIAPI
150NanoSecondDelay (
151 IN UINTN NanoSeconds
152 )
153{
154 InternalAcpiDelay (
155 (UINT32)DivU64x32 (
156 MultU64x32 (
157 NanoSeconds,
158 ACPI_TIMER_FREQUENCY
159 ),
160 1000000000u
161 )
162 );
163 return NanoSeconds;
164}
165
166/**
167 Retrieves the current value of a 64-bit free running performance counter.
168
169 Retrieves the current value of a 64-bit free running performance counter. The
170 counter can either count up by 1 or count down by 1. If the physical
171 performance counter counts by a larger increment, then the counter values
172 must be translated. The properties of the counter can be retrieved from
173 GetPerformanceCounterProperties().
174
175 @return The current value of the free running performance counter.
176
177**/
178UINT64
179EFIAPI
180GetPerformanceCounter (
181 VOID
182 )
183{
184 return (UINT64)InternalAcpiGetTimerTick ();
185}
186
187/**
188 Retrieves the 64-bit frequency in Hz and the range of performance counter
189 values.
190
191 If StartValue is not NULL, then the value that the performance counter starts
192 with immediately after is it rolls over is returned in StartValue. If
193 EndValue is not NULL, then the value that the performance counter end with
194 immediately before it rolls over is returned in EndValue. The 64-bit
195 frequency of the performance counter in Hz is always returned. If StartValue
196 is less than EndValue, then the performance counter counts up. If StartValue
197 is greater than EndValue, then the performance counter counts down. For
198 example, a 64-bit free running counter that counts up would have a StartValue
199 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
200 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
201
202 @param StartValue The value the performance counter starts with when it
203 rolls over.
204 @param EndValue The value that the performance counter ends with before
205 it rolls over.
206
207 @return The frequency in Hz.
208
209**/
210UINT64
211EFIAPI
212GetPerformanceCounterProperties (
213 OUT UINT64 *StartValue OPTIONAL,
214 OUT UINT64 *EndValue OPTIONAL
215 )
216{
217 if (StartValue != NULL) {
218 *StartValue = 0;
219 }
220
221 if (EndValue != NULL) {
222 *EndValue = ACPI_TIMER_COUNT_SIZE - 1;
223 }
224
225 return ACPI_TIMER_FREQUENCY;
226}
227
228/**
229 Converts elapsed ticks of performance counter to time in nanoseconds.
230
231 This function converts the elapsed ticks of running performance counter to
232 time value in unit of nanoseconds.
233
234 @param Ticks The number of elapsed ticks of running performance counter.
235
236 @return The elapsed time in nanoseconds.
237
238**/
239UINT64
240EFIAPI
241GetTimeInNanoSecond (
242 IN UINT64 Ticks
243 )
244{
245 UINT64 Frequency;
246 UINT64 NanoSeconds;
247 UINT64 Remainder;
248 INTN Shift;
249
250 Frequency = GetPerformanceCounterProperties (NULL, NULL);
251
252 //
253 // Ticks
254 // Time = --------- x 1,000,000,000
255 // Frequency
256 //
257 NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);
258
259 //
260 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
261 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
262 // i.e. highest bit set in Remainder should <= 33.
263 //
264 Shift = MAX (0, HighBitSet64 (Remainder) - 33);
265 Remainder = RShiftU64 (Remainder, (UINTN)Shift);
266 Frequency = RShiftU64 (Frequency, (UINTN)Shift);
267 NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);
268
269 return NanoSeconds;
270}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette