]>
Commit | Line | Data |
---|---|---|
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 | |
34 | #include <Library/TimerLib.h>\r | |
35 | #include <Library/BaseLib.h>\r | |
36 | #include <Library/IoLib.h>\r | |
37 | #include <Library/PciLib.h>\r | |
38 | #include <Library/PcdLib.h>\r | |
d50f6f8b | 39 | #include <Library/HobLib.h>\r |
c06ad33e | 40 | \r |
d50f6f8b SZ |
41 | #include <Guid/TscFrequency.h>\r |
42 | \r | |
43 | UINT64 mTscFrequency;\r | |
c06ad33e | 44 | \r |
c06ad33e | 45 | /** The constructor function determines the actual TSC frequency.\r |
46 | \r | |
d50f6f8b SZ |
47 | First, Get TSC frequency from TSC frequency GUID HOB,\r |
48 | If the HOB is not found, calculate it.\r | |
49 | \r | |
c06ad33e | 50 | The TSC counting frequency is determined by comparing how far it counts\r |
d50f6f8b | 51 | during a 1ms period as determined by the ACPI timer. The ACPI timer is\r |
c06ad33e | 52 | used because it counts at a known frequency.\r |
d50f6f8b | 53 | If ACPI I/O space not enabled, this function will enable it. Then the\r |
c06ad33e | 54 | TSC is sampled, followed by waiting for 3579 clocks of the ACPI timer, or 1ms.\r |
55 | The TSC is then sampled again. The difference multiplied by 1000 is the TSC\r | |
d50f6f8b SZ |
56 | frequency. There will be a small error because of the overhead of reading\r |
57 | the ACPI timer. An attempt is made to determine and compensate for this error.\r | |
58 | This function will always return EFI_SUCCESS.\r | |
c06ad33e | 59 | \r |
d50f6f8b | 60 | @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r |
c06ad33e | 61 | \r |
62 | **/\r | |
d50f6f8b | 63 | EFI_STATUS\r |
c06ad33e | 64 | EFIAPI\r |
d50f6f8b SZ |
65 | DxeTscTimerLibConstructor (\r |
66 | IN EFI_HANDLE ImageHandle,\r | |
67 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
c06ad33e | 68 | )\r |
69 | {\r | |
d50f6f8b SZ |
70 | EFI_HOB_GUID_TYPE *GuidHob;\r |
71 | VOID *DataInHob;\r | |
c06ad33e | 72 | UINT64 StartTSC;\r |
73 | UINT64 EndTSC;\r | |
74 | UINT32 TimerAddr;\r | |
75 | UINT32 Ticks;\r | |
76 | \r | |
d50f6f8b SZ |
77 | //\r |
78 | // Get TSC frequency from TSC frequency GUID HOB.\r | |
79 | //\r | |
80 | GuidHob = GetFirstGuidHob (&gEfiTscFrequencyGuid);\r | |
81 | if (GuidHob != NULL) {\r | |
82 | DataInHob = GET_GUID_HOB_DATA (GuidHob);\r | |
83 | mTscFrequency = * (UINT64 *) DataInHob;\r | |
84 | return EFI_SUCCESS;\r | |
85 | }\r | |
86 | \r | |
87 | //\r | |
88 | // TSC frequency GUID HOB is not found, calculate it.\r | |
89 | //\r | |
90 | \r | |
c06ad33e | 91 | //\r |
92 | // If ACPI I/O space is not enabled yet, program ACPI I/O base address and enable it.\r | |
93 | //\r | |
94 | if ((PciRead8 (PCI_ICH_LPC_ADDRESS (R_ICH_LPC_ACPI_CNT)) & B_ICH_LPC_ACPI_CNT_ACPI_EN) == 0) {\r | |
95 | PciWrite16 (PCI_ICH_LPC_ADDRESS (R_ICH_LPC_ACPI_BASE), PcdGet16 (PcdPerfPkgAcpiIoPortBaseAddress));\r | |
96 | PciOr8 (PCI_ICH_LPC_ADDRESS (R_ICH_LPC_ACPI_CNT), B_ICH_LPC_ACPI_CNT_ACPI_EN);\r | |
97 | }\r | |
98 | \r | |
d50f6f8b SZ |
99 | //\r |
100 | // ACPI I/O space should be enabled now, locate the ACPI Timer.\r | |
101 | // ACPI I/O base address maybe have be initialized by other driver with different value,\r | |
102 | // So get it from PCI space directly.\r | |
103 | //\r | |
104 | TimerAddr = ((PciRead16 (PCI_ICH_LPC_ADDRESS (R_ICH_LPC_ACPI_BASE))) & B_ICH_LPC_ACPI_BASE_BAR) + R_ACPI_PM1_TMR;\r | |
105 | Ticks = IoRead32 (TimerAddr) + (3579); // Set Ticks to 1ms in the future\r | |
c06ad33e | 106 | StartTSC = AsmReadTsc(); // Get base value for the TSC\r |
107 | //\r | |
108 | // Wait until the ACPI timer has counted 1ms.\r | |
109 | // Timer wrap-arounds are handled correctly by this function.\r | |
110 | // When the current ACPI timer value is greater than 'Ticks', the while loop will exit.\r | |
111 | //\r | |
d50f6f8b | 112 | while (((Ticks - IoRead32 (TimerAddr)) & BIT23) == 0) {\r |
c06ad33e | 113 | CpuPause();\r |
114 | }\r | |
115 | EndTSC = AsmReadTsc(); // TSC value 1ms later\r | |
116 | \r | |
117 | mTscFrequency = MultU64x32 (\r | |
118 | (EndTSC - StartTSC), // Number of TSC counts in 1ms\r | |
119 | 1000 // Number of ms in a second\r | |
120 | );\r | |
121 | //\r | |
122 | // mTscFrequency is now equal to the number of TSC counts per second\r | |
123 | //\r | |
d50f6f8b | 124 | return EFI_SUCCESS;\r |
c06ad33e | 125 | }\r |
126 | \r | |
127 | /** Stalls the CPU for at least the given number of ticks.\r | |
128 | \r | |
129 | Stalls the CPU for at least the given number of ticks. It's invoked by\r | |
130 | MicroSecondDelay() and NanoSecondDelay().\r | |
131 | \r | |
132 | @param[in] Delay A period of time to delay in ticks.\r | |
133 | \r | |
134 | **/\r | |
c06ad33e | 135 | VOID\r |
136 | InternalX86Delay (\r | |
137 | IN UINT64 Delay\r | |
138 | )\r | |
139 | {\r | |
140 | UINT64 Ticks;\r | |
141 | \r | |
142 | //\r | |
143 | // The target timer count is calculated here\r | |
144 | //\r | |
145 | Ticks = AsmReadTsc() + Delay;\r | |
146 | \r | |
147 | //\r | |
148 | // Wait until time out\r | |
149 | // Timer wrap-arounds are NOT handled correctly by this function.\r | |
150 | // Thus, this function must be called within 10 years of reset since\r | |
151 | // Intel guarantees a minimum of 10 years before the TSC wraps.\r | |
152 | //\r | |
153 | while (AsmReadTsc() <= Ticks) CpuPause();\r | |
154 | }\r | |
155 | \r | |
156 | /** Stalls the CPU for at least the specified number of MicroSeconds.\r | |
157 | \r | |
158 | @param[in] MicroSeconds The minimum number of microseconds to delay.\r | |
159 | \r | |
160 | @return The value of MicroSeconds input.\r | |
161 | \r | |
162 | **/\r | |
163 | UINTN\r | |
164 | EFIAPI\r | |
165 | MicroSecondDelay (\r | |
166 | IN UINTN MicroSeconds\r | |
167 | )\r | |
168 | {\r | |
169 | InternalX86Delay (\r | |
170 | DivU64x32 (\r | |
171 | MultU64x64 (\r | |
172 | mTscFrequency,\r | |
173 | MicroSeconds\r | |
174 | ),\r | |
175 | 1000000u\r | |
176 | )\r | |
177 | );\r | |
178 | return MicroSeconds;\r | |
179 | }\r | |
180 | \r | |
181 | /** Stalls the CPU for at least the specified number of NanoSeconds.\r | |
182 | \r | |
183 | @param[in] NanoSeconds The minimum number of nanoseconds to delay.\r | |
184 | \r | |
185 | @return The value of NanoSeconds input.\r | |
186 | \r | |
187 | **/\r | |
188 | UINTN\r | |
189 | EFIAPI\r | |
190 | NanoSecondDelay (\r | |
191 | IN UINTN NanoSeconds\r | |
192 | )\r | |
193 | {\r | |
194 | InternalX86Delay (\r | |
195 | DivU64x32 (\r | |
196 | MultU64x32 (\r | |
197 | mTscFrequency,\r | |
198 | (UINT32)NanoSeconds\r | |
199 | ),\r | |
200 | 1000000000u\r | |
201 | )\r | |
202 | );\r | |
203 | return NanoSeconds;\r | |
204 | }\r | |
205 | \r | |
206 | /** Retrieves the current value of the 64-bit free running Time-Stamp counter.\r | |
207 | \r | |
208 | The time-stamp counter (as implemented in the P6 family, Pentium, Pentium M,\r | |
209 | Pentium 4, Intel Xeon, Intel Core Solo and Intel Core Duo processors and\r | |
210 | later processors) is a 64-bit counter that is set to 0 following a RESET of\r | |
211 | the processor. Following a RESET, the counter increments even when the\r | |
212 | processor is halted by the HLT instruction or the external STPCLK# pin. Note\r | |
213 | that the assertion of the external DPSLP# pin may cause the time-stamp\r | |
214 | counter to stop.\r | |
215 | \r | |
216 | The properties of the counter can be retrieved by the\r | |
217 | GetPerformanceCounterProperties() function.\r | |
218 | \r | |
219 | @return The current value of the free running performance counter.\r | |
220 | \r | |
221 | **/\r | |
222 | UINT64\r | |
223 | EFIAPI\r | |
224 | GetPerformanceCounter (\r | |
225 | VOID\r | |
226 | )\r | |
227 | {\r | |
228 | return AsmReadTsc();\r | |
229 | }\r | |
230 | \r | |
231 | /** Retrieves the 64-bit frequency in Hz and the range of performance counter\r | |
232 | values.\r | |
233 | \r | |
234 | If StartValue is not NULL, then the value that the performance counter starts\r | |
235 | with, 0x0, is returned in StartValue. If EndValue is not NULL, then the value\r | |
236 | that the performance counter end with, 0xFFFFFFFFFFFFFFFF, is returned in\r | |
237 | EndValue.\r | |
238 | \r | |
239 | The 64-bit frequency of the performance counter, in Hz, is always returned.\r | |
240 | To determine average processor clock frequency, Intel recommends the use of\r | |
241 | EMON logic to count processor core clocks over the period of time for which\r | |
242 | the average is required.\r | |
243 | \r | |
244 | \r | |
245 | @param[out] StartValue Pointer to where the performance counter's starting value is saved, or NULL.\r | |
246 | @param[out] EndValue Pointer to where the performance counter's ending value is saved, or NULL.\r | |
247 | \r | |
248 | @return The frequency in Hz.\r | |
249 | \r | |
250 | **/\r | |
251 | UINT64\r | |
252 | EFIAPI\r | |
253 | GetPerformanceCounterProperties (\r | |
254 | OUT UINT64 *StartValue, OPTIONAL\r | |
255 | OUT UINT64 *EndValue OPTIONAL\r | |
256 | )\r | |
257 | {\r | |
258 | if (StartValue != NULL) {\r | |
259 | *StartValue = 0;\r | |
260 | }\r | |
261 | if (EndValue != NULL) {\r | |
262 | *EndValue = 0xFFFFFFFFFFFFFFFFull;\r | |
263 | }\r | |
264 | \r | |
265 | return mTscFrequency;\r | |
266 | }\r |