]> git.proxmox.com Git - mirror_edk2.git/blob - Vlv2TbltDevicePkg/Library/IntelPchAcpiTimerLib/IntelPchAcpiTimerLib.c
ArmVirtPkg/FdtPciHostBridgeLib: map ECAM and I/O spaces in GCD memory map
[mirror_edk2.git] / Vlv2TbltDevicePkg / Library / IntelPchAcpiTimerLib / IntelPchAcpiTimerLib.c
1 /** @file
2 ICH9 ACPI Timer implements one instance of Timer Library.
3
4 Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
5
6 This program and the accompanying materials are licensed and made available under
7 the terms and conditions of the BSD License that accompanies this distribution.
8 The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14
15 **/
16
17 #include "CommonHeader.h"
18
19 /**
20 The constructor function enables ACPI IO space.
21
22 If ACPI I/O space not enabled, this function will enable it.
23 It will always return RETURN_SUCCESS.
24
25 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
26
27 **/
28 RETURN_STATUS
29 EFIAPI
30 IntelPchAcpiTimerLibConstructor (
31 VOID
32 )
33 {
34 if ((PchLpcPciCfg8(R_PCH_LPC_ACPI_BASE) & B_PCH_LPC_ACPI_BASE_EN) == 0) {
35 //
36 // If ACPI I/O space is not enabled, program ACPI I/O base address and enable it.
37 //
38 MmioWrite16 (
39 MmPciAddress (
40 0,
41 DEFAULT_PCI_BUS_NUMBER_PCH,
42 PCI_DEVICE_NUMBER_PCH_LPC,
43 PCI_FUNCTION_NUMBER_PCH_LPC,
44 R_PCH_LPC_ACPI_BASE
45 ),
46 ((PcdGet16 (PcdPchAcpiIoPortBaseAddress) & B_PCH_LPC_ACPI_BASE_BAR) | B_PCH_LPC_ACPI_BASE_EN)
47 );
48 }
49 return RETURN_SUCCESS;
50 }
51
52 /**
53 Internal function to read the current tick counter of ACPI.
54
55 Internal function to read the current tick counter of ACPI.
56
57 @return The tick counter read.
58
59 **/
60 STATIC
61 UINT32
62 InternalAcpiGetTimerTick (
63 VOID
64 )
65 {
66 return IoRead32 (PcdGet16 (PcdPchAcpiIoPortBaseAddress) + R_PCH_ACPI_PM1_TMR);
67 }
68
69 /**
70 Stalls the CPU for at least the given number of ticks.
71
72 Stalls the CPU for at least the given number of ticks. It's invoked by
73 MicroSecondDelay() and NanoSecondDelay().
74
75 @param Delay A period of time to delay in ticks.
76
77 **/
78 STATIC
79 VOID
80 InternalAcpiDelay (
81 IN UINT32 Delay
82 )
83 {
84 UINT32 Ticks;
85 UINT32 Times;
86
87 Times = Delay >> 22;
88 Delay &= BIT22 - 1;
89 do {
90 //
91 // The target timer count is calculated here
92 //
93 Ticks = InternalAcpiGetTimerTick () + Delay;
94 Delay = BIT22;
95 //
96 // Wait until time out
97 // Delay >= 2^23 could not be handled by this function
98 // Timer wrap-arounds are handled correctly by this function
99 //
100 while (((Ticks - InternalAcpiGetTimerTick ()) & BIT23) == 0) {
101 CpuPause ();
102 }
103 } while (Times-- > 0);
104 }
105
106 /**
107 Stalls the CPU for at least the given number of microseconds.
108
109 Stalls the CPU for the number of microseconds specified by MicroSeconds.
110
111 @param MicroSeconds The minimum number of microseconds to delay.
112
113 @return MicroSeconds
114
115 **/
116 UINTN
117 EFIAPI
118 MicroSecondDelay (
119 IN UINTN MicroSeconds
120 )
121 {
122 InternalAcpiDelay (
123 (UINT32)DivU64x32 (
124 MultU64x32 (
125 MicroSeconds,
126 V_PCH_ACPI_PM1_TMR_FREQUENCY
127 ),
128 1000000u
129 )
130 );
131 return MicroSeconds;
132 }
133
134 /**
135 Stalls the CPU for at least the given number of nanoseconds.
136
137 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
138
139 @param NanoSeconds The minimum number of nanoseconds to delay.
140
141 @return NanoSeconds
142
143 **/
144 UINTN
145 EFIAPI
146 NanoSecondDelay (
147 IN UINTN NanoSeconds
148 )
149 {
150 InternalAcpiDelay (
151 (UINT32)DivU64x32 (
152 MultU64x32 (
153 NanoSeconds,
154 V_PCH_ACPI_PM1_TMR_FREQUENCY
155 ),
156 1000000000u
157 )
158 );
159 return NanoSeconds;
160 }
161
162 /**
163 Retrieves the current value of a 64-bit free running performance counter.
164
165 Retrieves the current value of a 64-bit free running performance counter. The
166 counter can either count up by 1 or count down by 1. If the physical
167 performance counter counts by a larger increment, then the counter values
168 must be translated. The properties of the counter can be retrieved from
169 GetPerformanceCounterProperties().
170
171 @return The current value of the free running performance counter.
172
173 **/
174 UINT64
175 EFIAPI
176 GetPerformanceCounter (
177 VOID
178 )
179 {
180 return (UINT64)InternalAcpiGetTimerTick ();
181 }
182
183 /**
184 Retrieves the 64-bit frequency in Hz and the range of performance counter
185 values.
186
187 If StartValue is not NULL, then the value that the performance counter starts
188 with immediately after is it rolls over is returned in StartValue. If
189 EndValue is not NULL, then the value that the performance counter end with
190 immediately before it rolls over is returned in EndValue. The 64-bit
191 frequency of the performance counter in Hz is always returned. If StartValue
192 is less than EndValue, then the performance counter counts up. If StartValue
193 is greater than EndValue, then the performance counter counts down. For
194 example, a 64-bit free running counter that counts up would have a StartValue
195 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
196 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
197
198 @param StartValue The value the performance counter starts with when it
199 rolls over.
200 @param EndValue The value that the performance counter ends with before
201 it rolls over.
202
203 @return The frequency in Hz.
204
205 **/
206 UINT64
207 EFIAPI
208 GetPerformanceCounterProperties (
209 OUT UINT64 *StartValue, OPTIONAL
210 OUT UINT64 *EndValue OPTIONAL
211 )
212 {
213 if (StartValue != NULL) {
214 *StartValue = 0;
215 }
216
217 if (EndValue != NULL) {
218 *EndValue = V_PCH_ACPI_PM1_TMR_MAX_VAL - 1;
219 }
220
221 return V_PCH_ACPI_PM1_TMR_FREQUENCY;
222 }
223
224 /**
225 Converts elapsed ticks of performance counter to time in nanoseconds.
226
227 This function converts the elapsed ticks of running performance counter to
228 time value in unit of nanoseconds.
229
230 @param Ticks The number of elapsed ticks of running performance counter.
231
232 @return The elapsed time in nanoseconds.
233
234 **/
235 UINT64
236 EFIAPI
237 GetTimeInNanoSecond (
238 IN UINT64 Ticks
239 )
240 {
241 UINT64 NanoSeconds;
242 UINT32 Remainder;
243
244 //
245 // Ticks
246 // Time = --------- x 1,000,000,000
247 // Frequency
248 //
249 NanoSeconds = MultU64x32 (DivU64x32Remainder (Ticks, V_PCH_ACPI_PM1_TMR_FREQUENCY, &Remainder), 1000000000u);
250
251 //
252 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
253 // will not overflow 64-bit.
254 //
255 NanoSeconds += DivU64x32 (MultU64x32 ((UINT64) Remainder, 1000000000u), V_PCH_ACPI_PM1_TMR_FREQUENCY);
256
257 return NanoSeconds;
258 }
259
260