]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.c
Add new API GetTimeInNanoSecond() to TimerLib to convert elapsed ticks to time in...
[mirror_edk2.git] / OvmfPkg / Library / AcpiTimerLib / AcpiTimerLib.c
1 /** @file
2 ACPI Timer implements one instance of Timer Library.
3
4 Copyright (c) 2008 - 2011, 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 <Base.h>
18 #include <Library/TimerLib.h>
19 #include <Library/BaseLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/PciLib.h>
22 #include <Library/DebugLib.h>
23
24 //
25 // PIIX4 Power Management Base Address
26 //
27 UINT32 mPmba = 0x400;
28
29 #define PCI_BAR_IO 0x1
30 #define ACPI_TIMER_FREQUENCY 3579545
31 #define ACPI_TIMER_COUNT_SIZE 0x01000000
32 #define ACPI_TIMER_OFFSET 0x8
33
34 /**
35 The constructor function enables ACPI IO space.
36
37 If ACPI I/O space not enabled, this function will enable it.
38 It will always return RETURN_SUCCESS.
39
40 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
41
42 **/
43 RETURN_STATUS
44 EFIAPI
45 AcpiTimerLibConstructor (
46 VOID
47 )
48 {
49 UINT8 Device;
50
51 Device = 1;
52 // Device = 7;
53
54 if (PciRead8 (PCI_LIB_ADDRESS (0,Device,3,0x80)) & 1) {
55 mPmba = PciRead32 (PCI_LIB_ADDRESS (0,Device,3,0x40));
56 ASSERT (mPmba & PCI_BAR_IO);
57 mPmba &= ~PCI_BAR_IO;
58 } else {
59 PciAndThenOr32 (PCI_LIB_ADDRESS (0,Device,3,0x40),
60 (UINT32) ~0xfc0, mPmba);
61 PciOr8 (PCI_LIB_ADDRESS (0,Device,3,0x04), 0x01);
62 }
63
64 //
65 // ACPI Timer enable is in Bus 0, Device ?, Function 3
66 //
67 PciOr8 (PCI_LIB_ADDRESS (0,Device,3,0x80), 0x01);
68 return RETURN_SUCCESS;
69 }
70
71 /**
72 Internal function to read the current tick counter of ACPI.
73
74 Internal function to read the current tick counter of ACPI.
75
76 @return The tick counter read.
77
78 **/
79 STATIC
80 UINT32
81 InternalAcpiGetTimerTick (
82 VOID
83 )
84 {
85 return IoRead32 (mPmba + ACPI_TIMER_OFFSET);
86 }
87
88 /**
89 Stalls the CPU for at least the given number of ticks.
90
91 Stalls the CPU for at least the given number of ticks. It's invoked by
92 MicroSecondDelay() and NanoSecondDelay().
93
94 @param Delay A period of time to delay in ticks.
95
96 **/
97 STATIC
98 VOID
99 InternalAcpiDelay (
100 IN UINT32 Delay
101 )
102 {
103 UINT32 Ticks;
104 UINT32 Times;
105
106 Times = Delay >> 22;
107 Delay &= BIT22 - 1;
108 do {
109 //
110 // The target timer count is calculated here
111 //
112 Ticks = InternalAcpiGetTimerTick () + Delay;
113 Delay = BIT22;
114 //
115 // Wait until time out
116 // Delay >= 2^23 could not be handled by this function
117 // Timer wrap-arounds are handled correctly by this function
118 //
119 while (((Ticks - InternalAcpiGetTimerTick ()) & BIT23) == 0) {
120 CpuPause ();
121 }
122 } while (Times-- > 0);
123 }
124
125 /**
126 Stalls the CPU for at least the given number of microseconds.
127
128 Stalls the CPU for the number of microseconds specified by MicroSeconds.
129
130 @param MicroSeconds The minimum number of microseconds to delay.
131
132 @return MicroSeconds
133
134 **/
135 UINTN
136 EFIAPI
137 MicroSecondDelay (
138 IN UINTN MicroSeconds
139 )
140 {
141 InternalAcpiDelay (
142 (UINT32)DivU64x32 (
143 MultU64x32 (
144 MicroSeconds,
145 ACPI_TIMER_FREQUENCY
146 ),
147 1000000u
148 )
149 );
150 return MicroSeconds;
151 }
152
153 /**
154 Stalls the CPU for at least the given number of nanoseconds.
155
156 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
157
158 @param NanoSeconds The minimum number of nanoseconds to delay.
159
160 @return NanoSeconds
161
162 **/
163 UINTN
164 EFIAPI
165 NanoSecondDelay (
166 IN UINTN NanoSeconds
167 )
168 {
169 InternalAcpiDelay (
170 (UINT32)DivU64x32 (
171 MultU64x32 (
172 NanoSeconds,
173 ACPI_TIMER_FREQUENCY
174 ),
175 1000000000u
176 )
177 );
178 return NanoSeconds;
179 }
180
181 /**
182 Retrieves the current value of a 64-bit free running performance counter.
183
184 Retrieves the current value of a 64-bit free running performance counter. The
185 counter can either count up by 1 or count down by 1. If the physical
186 performance counter counts by a larger increment, then the counter values
187 must be translated. The properties of the counter can be retrieved from
188 GetPerformanceCounterProperties().
189
190 @return The current value of the free running performance counter.
191
192 **/
193 UINT64
194 EFIAPI
195 GetPerformanceCounter (
196 VOID
197 )
198 {
199 return (UINT64)InternalAcpiGetTimerTick ();
200 }
201
202 /**
203 Retrieves the 64-bit frequency in Hz and the range of performance counter
204 values.
205
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.
216
217 @param StartValue The value the performance counter starts with when it
218 rolls over.
219 @param EndValue The value that the performance counter ends with before
220 it rolls over.
221
222 @return The frequency in Hz.
223
224 **/
225 UINT64
226 EFIAPI
227 GetPerformanceCounterProperties (
228 OUT UINT64 *StartValue, OPTIONAL
229 OUT UINT64 *EndValue OPTIONAL
230 )
231 {
232 if (StartValue != NULL) {
233 *StartValue = 0;
234 }
235
236 if (EndValue != NULL) {
237 *EndValue = ACPI_TIMER_COUNT_SIZE - 1;
238 }
239
240 return ACPI_TIMER_FREQUENCY;
241 }
242
243 /**
244 Converts elapsed ticks of performance counter to time in nanoseconds.
245
246 This function converts the elapsed ticks of running performance counter to
247 time value in unit of nanoseconds.
248
249 @param Ticks The number of elapsed ticks of running performance counter.
250
251 @return The elapsed time in nanoseconds.
252
253 **/
254 UINT64
255 EFIAPI
256 GetTimeInNanoSecond (
257 IN UINT64 Ticks
258 )
259 {
260 UINT64 NanoSeconds;
261 UINT32 Remainder;
262
263 //
264 // Ticks
265 // Time = --------- x 1,000,000,000
266 // Frequency
267 //
268 NanoSeconds = MultU64x32 (DivU64x32Remainder (Ticks, ACPI_TIMER_FREQUENCY, &Remainder), 1000000000u);
269
270 //
271 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
272 // will not overflow 64-bit.
273 //
274 NanoSeconds += DivU64x32 (MultU64x32 ((UINT64) Remainder, 1000000000u), ACPI_TIMER_FREQUENCY);
275
276 return NanoSeconds;
277 }