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