]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.c
OvmfPkg: QemuFlashFvbServicesRuntimeDxe: remove FvbDevLock field
[mirror_edk2.git] / OvmfPkg / Library / AcpiTimerLib / AcpiTimerLib.c
1 /** @file
2 ACPI Timer implements one instance of Timer Library.
3
4 Copyright (c) 2008 - 2012, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2011, Andrei Warkentin <andreiw@motorola.com>
6
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
11
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.
14
15 **/
16
17 #include <Library/DebugLib.h>
18 #include <Library/BaseLib.h>
19 #include <IndustryStandard/Acpi.h>
20
21 #include "AcpiTimerLib.h"
22
23 //
24 // The ACPI Time is a 24-bit counter
25 //
26 #define ACPI_TIMER_COUNT_SIZE BIT24
27
28 /**
29 Stalls the CPU for at least the given number of ticks.
30
31 Stalls the CPU for at least the given number of ticks. It's invoked by
32 MicroSecondDelay() and NanoSecondDelay().
33
34 @param Delay A period of time to delay in ticks.
35
36 **/
37 VOID
38 InternalAcpiDelay (
39 IN UINT32 Delay
40 )
41 {
42 UINT32 Ticks;
43 UINT32 Times;
44
45 Times = Delay >> 22;
46 Delay &= BIT22 - 1;
47 do {
48 //
49 // The target timer count is calculated here
50 //
51 Ticks = InternalAcpiGetTimerTick () + Delay;
52 Delay = BIT22;
53 //
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
57 //
58 while (((Ticks - InternalAcpiGetTimerTick ()) & BIT23) == 0) {
59 CpuPause ();
60 }
61 } while (Times-- > 0);
62 }
63
64 /**
65 Stalls the CPU for at least the given number of microseconds.
66
67 Stalls the CPU for the number of microseconds specified by MicroSeconds.
68
69 @param MicroSeconds The minimum number of microseconds to delay.
70
71 @return MicroSeconds
72
73 **/
74 UINTN
75 EFIAPI
76 MicroSecondDelay (
77 IN UINTN MicroSeconds
78 )
79 {
80 InternalAcpiDelay (
81 (UINT32)DivU64x32 (
82 MultU64x32 (
83 MicroSeconds,
84 ACPI_TIMER_FREQUENCY
85 ),
86 1000000u
87 )
88 );
89 return MicroSeconds;
90 }
91
92 /**
93 Stalls the CPU for at least the given number of nanoseconds.
94
95 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
96
97 @param NanoSeconds The minimum number of nanoseconds to delay.
98
99 @return NanoSeconds
100
101 **/
102 UINTN
103 EFIAPI
104 NanoSecondDelay (
105 IN UINTN NanoSeconds
106 )
107 {
108 InternalAcpiDelay (
109 (UINT32)DivU64x32 (
110 MultU64x32 (
111 NanoSeconds,
112 ACPI_TIMER_FREQUENCY
113 ),
114 1000000000u
115 )
116 );
117 return NanoSeconds;
118 }
119
120 /**
121 Retrieves the current value of a 64-bit free running performance counter.
122
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().
128
129 @return The current value of the free running performance counter.
130
131 **/
132 UINT64
133 EFIAPI
134 GetPerformanceCounter (
135 VOID
136 )
137 {
138 return (UINT64)InternalAcpiGetTimerTick ();
139 }
140
141 /**
142 Retrieves the 64-bit frequency in Hz and the range of performance counter
143 values.
144
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.
155
156 @param StartValue The value the performance counter starts with when it
157 rolls over.
158 @param EndValue The value that the performance counter ends with before
159 it rolls over.
160
161 @return The frequency in Hz.
162
163 **/
164 UINT64
165 EFIAPI
166 GetPerformanceCounterProperties (
167 OUT UINT64 *StartValue, OPTIONAL
168 OUT UINT64 *EndValue OPTIONAL
169 )
170 {
171 if (StartValue != NULL) {
172 *StartValue = 0;
173 }
174
175 if (EndValue != NULL) {
176 *EndValue = ACPI_TIMER_COUNT_SIZE - 1;
177 }
178
179 return ACPI_TIMER_FREQUENCY;
180 }
181
182 /**
183 Converts elapsed ticks of performance counter to time in nanoseconds.
184
185 This function converts the elapsed ticks of running performance counter to
186 time value in unit of nanoseconds.
187
188 @param Ticks The number of elapsed ticks of running performance counter.
189
190 @return The elapsed time in nanoseconds.
191
192 **/
193 UINT64
194 EFIAPI
195 GetTimeInNanoSecond (
196 IN UINT64 Ticks
197 )
198 {
199 UINT64 NanoSeconds;
200 UINT32 Remainder;
201
202 //
203 // Ticks
204 // Time = --------- x 1,000,000,000
205 // Frequency
206 //
207 NanoSeconds = MultU64x32 (DivU64x32Remainder (Ticks, ACPI_TIMER_FREQUENCY, &Remainder), 1000000000u);
208
209 //
210 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
211 // will not overflow 64-bit.
212 //
213 NanoSeconds += DivU64x32 (MultU64x32 ((UINT64) Remainder, 1000000000u), ACPI_TIMER_FREQUENCY);
214
215 return NanoSeconds;
216 }