]> git.proxmox.com Git - mirror_edk2.git/blame - PerformancePkg/Library/DxeTscTimerLib/DxeTscTimerLib.c
Allocate memory buffer to store TscFrequency, and InstallConfigurationTable with...
[mirror_edk2.git] / PerformancePkg / Library / DxeTscTimerLib / DxeTscTimerLib.c
CommitLineData
c06ad33e 1/** @file\r
d50f6f8b 2 A Dxe Timer Library implementation which uses the Time Stamp Counter in the processor.\r
c06ad33e 3\r
4 For Pentium 4 processors, Intel Xeon processors (family [0FH], models [03H and higher]);\r
5 for Intel Core Solo and Intel Core Duo processors (family [06H], model [0EH]);\r
6 for the Intel Xeon processor 5100 series and Intel Core 2 Duo processors (family [06H], model [0FH]);\r
7 for Intel Core 2 and Intel Xeon processors (family [06H], display_model [17H]);\r
8 for Intel Atom processors (family [06H], display_model [1CH]):\r
9 the time-stamp counter increments at a constant rate.\r
10 That rate may be set by the maximum core-clock to bus-clock ratio of the processor or may be set by\r
11 the maximum resolved frequency at which the processor is booted. The maximum resolved frequency may\r
12 differ from the maximum qualified frequency of the processor.\r
13\r
14 The specific processor configuration determines the behavior. Constant TSC behavior ensures that the\r
15 duration of each clock tick is uniform and supports the use of the TSC as a wall clock timer even if\r
d50f6f8b 16 the processor core changes frequency. This is the architectural behavior moving forward.\r
c06ad33e 17\r
24cd83a7 18 A Processor's support for invariant TSC is indicated by CPUID.0x80000007.EDX[8].\r
c06ad33e 19\r
d50f6f8b 20 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
c06ad33e 21 This program and the accompanying materials\r
22 are licensed and made available under the terms and conditions of the BSD License\r
d50f6f8b 23 which accompanies this distribution. The full text of the license may be found at\r
c06ad33e 24 http://opensource.org/licenses/bsd-license.php\r
25\r
26 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
27 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
28\r
29**/\r
30\r
d50f6f8b 31#include <PiDxe.h>\r
c06ad33e 32#include <Ich/GenericIch.h>\r
33\r
a176509a 34#include <Library/UefiBootServicesTableLib.h>\r
c06ad33e 35#include <Library/TimerLib.h>\r
36#include <Library/BaseLib.h>\r
37#include <Library/IoLib.h>\r
38#include <Library/PciLib.h>\r
39#include <Library/PcdLib.h>\r
a176509a 40#include <Library/UefiLib.h>\r
4ae5165c 41#include <Library/DebugLib.h>\r
c06ad33e 42\r
d50f6f8b
SZ
43#include <Guid/TscFrequency.h>\r
44\r
a176509a 45UINT64 mTscFrequency;\r
c06ad33e 46\r
c06ad33e 47/** The constructor function determines the actual TSC frequency.\r
48\r
a176509a
SZ
49 First, Get TSC frequency from system configuration table with TSC frequency GUID,\r
50 if the table is not found, install it.\r
d50f6f8b 51\r
c06ad33e 52 The TSC counting frequency is determined by comparing how far it counts\r
d50f6f8b 53 during a 1ms period as determined by the ACPI timer. The ACPI timer is\r
c06ad33e 54 used because it counts at a known frequency.\r
d50f6f8b 55 If ACPI I/O space not enabled, this function will enable it. Then the\r
c06ad33e 56 TSC is sampled, followed by waiting for 3579 clocks of the ACPI timer, or 1ms.\r
57 The TSC is then sampled again. The difference multiplied by 1000 is the TSC\r
d50f6f8b
SZ
58 frequency. There will be a small error because of the overhead of reading\r
59 the ACPI timer. An attempt is made to determine and compensate for this error.\r
60 This function will always return EFI_SUCCESS.\r
c06ad33e 61\r
d66e9f0d
SZ
62 @param ImageHandle The firmware allocated handle for the EFI image.\r
63 @param SystemTable A pointer to the EFI System Table.\r
64\r
d50f6f8b 65 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
c06ad33e 66\r
67**/\r
d50f6f8b 68EFI_STATUS\r
c06ad33e 69EFIAPI\r
d50f6f8b
SZ
70DxeTscTimerLibConstructor (\r
71 IN EFI_HANDLE ImageHandle,\r
72 IN EFI_SYSTEM_TABLE *SystemTable\r
c06ad33e 73 )\r
74{\r
a176509a
SZ
75 EFI_STATUS Status;\r
76 UINT64 *TscFrequency;\r
c06ad33e 77 UINT64 StartTSC;\r
78 UINT64 EndTSC;\r
79 UINT32 TimerAddr;\r
80 UINT32 Ticks;\r
81\r
d50f6f8b 82 //\r
a176509a 83 // Get TSC frequency from system configuration table with TSC frequency GUID.\r
d50f6f8b 84 //\r
a176509a
SZ
85 Status = EfiGetSystemConfigurationTable (&gEfiTscFrequencyGuid, (VOID **) &TscFrequency);\r
86 if (Status == EFI_SUCCESS) {\r
87 mTscFrequency = *TscFrequency;\r
d50f6f8b
SZ
88 return EFI_SUCCESS;\r
89 }\r
90\r
91 //\r
a176509a 92 // TSC frequency GUID system configuration table is not found, install it.\r
d50f6f8b
SZ
93 //\r
94\r
c06ad33e 95 //\r
96 // If ACPI I/O space is not enabled yet, program ACPI I/O base address and enable it.\r
97 //\r
98 if ((PciRead8 (PCI_ICH_LPC_ADDRESS (R_ICH_LPC_ACPI_CNT)) & B_ICH_LPC_ACPI_CNT_ACPI_EN) == 0) {\r
99 PciWrite16 (PCI_ICH_LPC_ADDRESS (R_ICH_LPC_ACPI_BASE), PcdGet16 (PcdPerfPkgAcpiIoPortBaseAddress));\r
100 PciOr8 (PCI_ICH_LPC_ADDRESS (R_ICH_LPC_ACPI_CNT), B_ICH_LPC_ACPI_CNT_ACPI_EN);\r
101 }\r
102\r
d50f6f8b
SZ
103 //\r
104 // ACPI I/O space should be enabled now, locate the ACPI Timer.\r
105 // ACPI I/O base address maybe have be initialized by other driver with different value,\r
106 // So get it from PCI space directly.\r
107 //\r
108 TimerAddr = ((PciRead16 (PCI_ICH_LPC_ADDRESS (R_ICH_LPC_ACPI_BASE))) & B_ICH_LPC_ACPI_BASE_BAR) + R_ACPI_PM1_TMR;\r
109 Ticks = IoRead32 (TimerAddr) + (3579); // Set Ticks to 1ms in the future\r
c06ad33e 110 StartTSC = AsmReadTsc(); // Get base value for the TSC\r
111 //\r
112 // Wait until the ACPI timer has counted 1ms.\r
113 // Timer wrap-arounds are handled correctly by this function.\r
114 // When the current ACPI timer value is greater than 'Ticks', the while loop will exit.\r
115 //\r
d50f6f8b 116 while (((Ticks - IoRead32 (TimerAddr)) & BIT23) == 0) {\r
c06ad33e 117 CpuPause();\r
118 }\r
119 EndTSC = AsmReadTsc(); // TSC value 1ms later\r
120\r
4ae5165c
SZ
121 Status = gBS->AllocatePool (EfiBootServicesData, sizeof (UINT64), &TscFrequency);\r
122 ASSERT_EFI_ERROR (Status);\r
123\r
124 *TscFrequency = MultU64x32 (\r
125 (EndTSC - StartTSC), // Number of TSC counts in 1ms\r
126 1000 // Number of ms in a second\r
127 );\r
c06ad33e 128 //\r
4ae5165c 129 // TscFrequency now points to the number of TSC counts per second, install system configuration table for it.\r
c06ad33e 130 //\r
4ae5165c 131 gBS->InstallConfigurationTable (&gEfiTscFrequencyGuid, TscFrequency);\r
a176509a 132\r
4ae5165c 133 mTscFrequency = *TscFrequency;\r
d50f6f8b 134 return EFI_SUCCESS;\r
c06ad33e 135}\r
136\r
137/** Stalls the CPU for at least the given number of ticks.\r
138\r
139 Stalls the CPU for at least the given number of ticks. It's invoked by\r
140 MicroSecondDelay() and NanoSecondDelay().\r
141\r
142 @param[in] Delay A period of time to delay in ticks.\r
143\r
144**/\r
c06ad33e 145VOID\r
146InternalX86Delay (\r
147 IN UINT64 Delay\r
148 )\r
149{\r
150 UINT64 Ticks;\r
151\r
152 //\r
153 // The target timer count is calculated here\r
154 //\r
155 Ticks = AsmReadTsc() + Delay;\r
156\r
157 //\r
158 // Wait until time out\r
159 // Timer wrap-arounds are NOT handled correctly by this function.\r
160 // Thus, this function must be called within 10 years of reset since\r
161 // Intel guarantees a minimum of 10 years before the TSC wraps.\r
162 //\r
163 while (AsmReadTsc() <= Ticks) CpuPause();\r
164}\r
165\r
166/** Stalls the CPU for at least the specified number of MicroSeconds.\r
167\r
168 @param[in] MicroSeconds The minimum number of microseconds to delay.\r
169\r
170 @return The value of MicroSeconds input.\r
171\r
172**/\r
173UINTN\r
174EFIAPI\r
175MicroSecondDelay (\r
176 IN UINTN MicroSeconds\r
177 )\r
178{\r
179 InternalX86Delay (\r
180 DivU64x32 (\r
181 MultU64x64 (\r
182 mTscFrequency,\r
183 MicroSeconds\r
184 ),\r
185 1000000u\r
186 )\r
187 );\r
188 return MicroSeconds;\r
189}\r
190\r
191/** Stalls the CPU for at least the specified number of NanoSeconds.\r
192\r
193 @param[in] NanoSeconds The minimum number of nanoseconds to delay.\r
194\r
195 @return The value of NanoSeconds input.\r
196\r
197**/\r
198UINTN\r
199EFIAPI\r
200NanoSecondDelay (\r
201 IN UINTN NanoSeconds\r
202 )\r
203{\r
204 InternalX86Delay (\r
205 DivU64x32 (\r
206 MultU64x32 (\r
207 mTscFrequency,\r
208 (UINT32)NanoSeconds\r
209 ),\r
210 1000000000u\r
211 )\r
212 );\r
213 return NanoSeconds;\r
214}\r
215\r
216/** Retrieves the current value of the 64-bit free running Time-Stamp counter.\r
217\r
218 The time-stamp counter (as implemented in the P6 family, Pentium, Pentium M,\r
219 Pentium 4, Intel Xeon, Intel Core Solo and Intel Core Duo processors and\r
220 later processors) is a 64-bit counter that is set to 0 following a RESET of\r
221 the processor. Following a RESET, the counter increments even when the\r
222 processor is halted by the HLT instruction or the external STPCLK# pin. Note\r
223 that the assertion of the external DPSLP# pin may cause the time-stamp\r
224 counter to stop.\r
225\r
226 The properties of the counter can be retrieved by the\r
227 GetPerformanceCounterProperties() function.\r
228\r
229 @return The current value of the free running performance counter.\r
230\r
231**/\r
232UINT64\r
233EFIAPI\r
234GetPerformanceCounter (\r
235 VOID\r
236 )\r
237{\r
238 return AsmReadTsc();\r
239}\r
240\r
241/** Retrieves the 64-bit frequency in Hz and the range of performance counter\r
242 values.\r
243\r
244 If StartValue is not NULL, then the value that the performance counter starts\r
245 with, 0x0, is returned in StartValue. If EndValue is not NULL, then the value\r
246 that the performance counter end with, 0xFFFFFFFFFFFFFFFF, is returned in\r
247 EndValue.\r
248\r
249 The 64-bit frequency of the performance counter, in Hz, is always returned.\r
250 To determine average processor clock frequency, Intel recommends the use of\r
251 EMON logic to count processor core clocks over the period of time for which\r
252 the average is required.\r
253\r
254\r
255 @param[out] StartValue Pointer to where the performance counter's starting value is saved, or NULL.\r
256 @param[out] EndValue Pointer to where the performance counter's ending value is saved, or NULL.\r
257\r
258 @return The frequency in Hz.\r
259\r
260**/\r
261UINT64\r
262EFIAPI\r
263GetPerformanceCounterProperties (\r
264 OUT UINT64 *StartValue, OPTIONAL\r
265 OUT UINT64 *EndValue OPTIONAL\r
266 )\r
267{\r
268 if (StartValue != NULL) {\r
269 *StartValue = 0;\r
270 }\r
271 if (EndValue != NULL) {\r
272 *EndValue = 0xFFFFFFFFFFFFFFFFull;\r
273 }\r
274\r
275 return mTscFrequency;\r
276}\r