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