VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.c@ 48674

Last change on this file since 48674 was 48674, checked in by vboxsync, 11 years ago

EFI: Export newly imported tinaocore UEFI sources to OSE.

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
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 <[email protected]>
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//
27UINT32 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**/
43RETURN_STATUS
44EFIAPI
45AcpiTimerLibConstructor (
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**/
79STATIC
80UINT32
81InternalAcpiGetTimerTick (
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**/
97STATIC
98VOID
99InternalAcpiDelay (
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**/
135UINTN
136EFIAPI
137MicroSecondDelay (
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**/
163UINTN
164EFIAPI
165NanoSecondDelay (
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**/
193UINT64
194EFIAPI
195GetPerformanceCounter (
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**/
225UINT64
226EFIAPI
227GetPerformanceCounterProperties (
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**/
254UINT64
255EFIAPI
256GetTimeInNanoSecond (
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}
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