]>
Commit | Line | Data |
---|---|---|
18c97f53 | 1 | /** @file\r |
2 | Timer Architectural Protocol as defined in the DXE CIS\r | |
eb16e240 | 3 | \r |
5a702acd | 4 | Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>\r |
e1d302e5 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
eb16e240 | 6 | \r |
18c97f53 | 7 | **/\r |
eb16e240 | 8 | \r |
9 | #include "Timer.h"\r | |
10 | \r | |
11 | //\r | |
12 | // The handle onto which the Timer Architectural Protocol will be installed\r | |
13 | //\r | |
14 | EFI_HANDLE mTimerHandle = NULL;\r | |
15 | \r | |
16 | //\r | |
17 | // The Timer Architectural Protocol that this driver produces\r | |
18 | //\r | |
19 | EFI_TIMER_ARCH_PROTOCOL mTimer = {\r | |
20 | TimerDriverRegisterHandler,\r | |
21 | TimerDriverSetTimerPeriod,\r | |
22 | TimerDriverGetTimerPeriod,\r | |
23 | TimerDriverGenerateSoftInterrupt\r | |
24 | };\r | |
25 | \r | |
26 | //\r | |
27 | // Pointer to the CPU Architectural Protocol instance\r | |
28 | //\r | |
29 | EFI_CPU_ARCH_PROTOCOL *mCpu;\r | |
30 | \r | |
eb16e240 | 31 | //\r |
32 | // Pointer to the Legacy 8259 Protocol instance\r | |
33 | //\r | |
34 | EFI_LEGACY_8259_PROTOCOL *mLegacy8259;\r | |
35 | \r | |
36 | //\r | |
37 | // The notification function to call on every timer interrupt.\r | |
38 | // A bug in the compiler prevents us from initializing this here.\r | |
39 | //\r | |
2b7d16cf | 40 | EFI_TIMER_NOTIFY mTimerNotifyFunction;\r |
eb16e240 | 41 | \r |
42 | //\r | |
43 | // The current period of the timer interrupt\r | |
44 | //\r | |
45 | volatile UINT64 mTimerPeriod = 0;\r | |
46 | \r | |
47 | //\r | |
48 | // Worker Functions\r | |
49 | //\r | |
18c97f53 | 50 | /**\r |
51 | Sets the counter value for Timer #0 in a legacy 8254 timer.\r | |
52 | \r | |
53 | @param Count The 16-bit counter value to program into Timer #0 of the legacy 8254 timer.\r | |
54 | **/\r | |
eb16e240 | 55 | VOID\r |
56 | SetPitCount (\r | |
57 | IN UINT16 Count\r | |
58 | )\r | |
eb16e240 | 59 | {\r |
2952f72d | 60 | IoWrite8 (TIMER_CONTROL_PORT, 0x36);\r |
61 | IoWrite8 (TIMER0_COUNT_PORT, (UINT8)(Count & 0xff));\r | |
62 | IoWrite8 (TIMER0_COUNT_PORT, (UINT8)((Count >> 8) & 0xff));\r | |
eb16e240 | 63 | }\r |
64 | \r | |
18c97f53 | 65 | /**\r |
24115e44 | 66 | 8254 Timer #0 Interrupt Handler.\r |
18c97f53 | 67 | \r |
619ad10f TH |
68 | @param InterruptType The type of interrupt that occurred\r |
69 | @param SystemContext A pointer to the system context when the interrupt occurred\r | |
18c97f53 | 70 | **/\r |
eb16e240 | 71 | VOID\r |
72 | EFIAPI\r | |
73 | TimerInterruptHandler (\r | |
74 | IN EFI_EXCEPTION_TYPE InterruptType,\r | |
75 | IN EFI_SYSTEM_CONTEXT SystemContext\r | |
76 | )\r | |
eb16e240 | 77 | {\r |
78 | EFI_TPL OriginalTPL;\r | |
79 | \r | |
80 | OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r | |
81 | \r | |
82 | mLegacy8259->EndOfInterrupt (mLegacy8259, Efi8259Irq0);\r | |
83 | \r | |
24115e44 | 84 | if (mTimerNotifyFunction != NULL) {\r |
eb16e240 | 85 | //\r |
24115e44 | 86 | // @bug : This does not handle missed timer interrupts\r |
eb16e240 | 87 | //\r |
88 | mTimerNotifyFunction (mTimerPeriod);\r | |
89 | }\r | |
90 | \r | |
91 | gBS->RestoreTPL (OriginalTPL);\r | |
92 | }\r | |
93 | \r | |
18c97f53 | 94 | /**\r |
95 | \r | |
96 | This function registers the handler NotifyFunction so it is called every time\r | |
97 | the timer interrupt fires. It also passes the amount of time since the last\r | |
98 | handler call to the NotifyFunction. If NotifyFunction is NULL, then the\r | |
99 | handler is unregistered. If the handler is registered, then EFI_SUCCESS is\r | |
100 | returned. If the CPU does not support registering a timer interrupt handler,\r | |
101 | then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler\r | |
102 | when a handler is already registered, then EFI_ALREADY_STARTED is returned.\r | |
103 | If an attempt is made to unregister a handler when a handler is not registered,\r | |
104 | then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to\r | |
105 | register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR\r | |
106 | is returned.\r | |
107 | \r | |
108 | \r | |
109 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
110 | @param NotifyFunction The function to call when a timer interrupt fires. This\r | |
111 | function executes at TPL_HIGH_LEVEL. The DXE Core will\r | |
112 | register a handler for the timer interrupt, so it can know\r | |
113 | how much time has passed. This information is used to\r | |
114 | signal timer based events. NULL will unregister the handler.\r | |
115 | \r | |
116 | @retval EFI_SUCCESS The timer handler was registered.\r | |
117 | @retval EFI_UNSUPPORTED The platform does not support timer interrupts.\r | |
118 | @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already\r | |
119 | registered.\r | |
120 | @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not\r | |
121 | previously registered.\r | |
122 | @retval EFI_DEVICE_ERROR The timer handler could not be registered.\r | |
123 | \r | |
124 | **/\r | |
eb16e240 | 125 | EFI_STATUS\r |
126 | EFIAPI\r | |
127 | TimerDriverRegisterHandler (\r | |
128 | IN EFI_TIMER_ARCH_PROTOCOL *This,\r | |
129 | IN EFI_TIMER_NOTIFY NotifyFunction\r | |
130 | )\r | |
eb16e240 | 131 | {\r |
132 | //\r | |
133 | // Check for invalid parameters\r | |
134 | //\r | |
135 | if (NotifyFunction == NULL && mTimerNotifyFunction == NULL) {\r | |
136 | return EFI_INVALID_PARAMETER;\r | |
137 | }\r | |
138 | \r | |
139 | if (NotifyFunction != NULL && mTimerNotifyFunction != NULL) {\r | |
140 | return EFI_ALREADY_STARTED;\r | |
141 | }\r | |
142 | \r | |
143 | mTimerNotifyFunction = NotifyFunction;\r | |
144 | \r | |
145 | return EFI_SUCCESS;\r | |
146 | }\r | |
147 | \r | |
18c97f53 | 148 | /**\r |
149 | \r | |
150 | This function adjusts the period of timer interrupts to the value specified\r | |
151 | by TimerPeriod. If the timer period is updated, then the selected timer\r | |
152 | period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If\r | |
153 | the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.\r | |
154 | If an error occurs while attempting to update the timer period, then the\r | |
155 | timer hardware will be put back in its state prior to this call, and\r | |
156 | EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt\r | |
157 | is disabled. This is not the same as disabling the CPU's interrupts.\r | |
158 | Instead, it must either turn off the timer hardware, or it must adjust the\r | |
159 | interrupt controller so that a CPU interrupt is not generated when the timer\r | |
160 | interrupt fires.\r | |
161 | \r | |
162 | \r | |
163 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
164 | @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If\r | |
165 | the timer hardware is not programmable, then EFI_UNSUPPORTED is\r | |
166 | returned. If the timer is programmable, then the timer period\r | |
167 | will be rounded up to the nearest timer period that is supported\r | |
168 | by the timer hardware. If TimerPeriod is set to 0, then the\r | |
169 | timer interrupts will be disabled.\r | |
170 | \r | |
171 | @retval EFI_SUCCESS The timer period was changed.\r | |
172 | @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.\r | |
173 | @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.\r | |
174 | \r | |
175 | **/\r | |
eb16e240 | 176 | EFI_STATUS\r |
177 | EFIAPI\r | |
178 | TimerDriverSetTimerPeriod (\r | |
179 | IN EFI_TIMER_ARCH_PROTOCOL *This,\r | |
180 | IN UINT64 TimerPeriod\r | |
181 | )\r | |
eb16e240 | 182 | {\r |
183 | UINT64 TimerCount;\r | |
184 | \r | |
185 | //\r | |
186 | // The basic clock is 1.19318 MHz or 0.119318 ticks per 100 ns.\r | |
187 | // TimerPeriod * 0.119318 = 8254 timer divisor. Using integer arithmetic\r | |
188 | // TimerCount = (TimerPeriod * 119318)/1000000.\r | |
189 | //\r | |
190 | // Round up to next highest integer. This guarantees that the timer is\r | |
191 | // equal to or slightly longer than the requested time.\r | |
192 | // TimerCount = ((TimerPeriod * 119318) + 500000)/1000000\r | |
193 | //\r | |
194 | // Note that a TimerCount of 0 is equivalent to a count of 65,536\r | |
195 | //\r | |
196 | // Since TimerCount is limited to 16 bits for IA32, TimerPeriod is limited\r | |
197 | // to 20 bits.\r | |
198 | //\r | |
199 | if (TimerPeriod == 0) {\r | |
200 | //\r | |
201 | // Disable timer interrupt for a TimerPeriod of 0\r | |
202 | //\r | |
203 | mLegacy8259->DisableIrq (mLegacy8259, Efi8259Irq0);\r | |
204 | } else {\r | |
2952f72d | 205 | \r |
eb16e240 | 206 | //\r |
207 | // Convert TimerPeriod into 8254 counts\r | |
208 | //\r | |
394bbc59 | 209 | TimerCount = DivU64x32 (MultU64x32 (119318, (UINT32) TimerPeriod) + 500000, 1000000);\r |
eb16e240 | 210 | \r |
211 | //\r | |
212 | // Check for overflow\r | |
213 | //\r | |
214 | if (TimerCount >= 65536) {\r | |
215 | TimerCount = 0;\r | |
76cd9a03 | 216 | TimerPeriod = MAX_TIMER_TICK_DURATION;\r |
eb16e240 | 217 | }\r |
218 | //\r | |
219 | // Program the 8254 timer with the new count value\r | |
220 | //\r | |
221 | SetPitCount ((UINT16) TimerCount);\r | |
222 | \r | |
223 | //\r | |
224 | // Enable timer interrupt\r | |
225 | //\r | |
226 | mLegacy8259->EnableIrq (mLegacy8259, Efi8259Irq0, FALSE);\r | |
227 | }\r | |
228 | //\r | |
229 | // Save the new timer period\r | |
230 | //\r | |
231 | mTimerPeriod = TimerPeriod;\r | |
232 | \r | |
233 | return EFI_SUCCESS;\r | |
234 | }\r | |
235 | \r | |
18c97f53 | 236 | /**\r |
eb16e240 | 237 | \r |
18c97f53 | 238 | This function retrieves the period of timer interrupts in 100 ns units,\r |
239 | returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod\r | |
240 | is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is\r | |
eb16e240 | 241 | returned, then the timer is currently disabled.\r |
242 | \r | |
eb16e240 | 243 | \r |
18c97f53 | 244 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r |
245 | @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If\r | |
246 | 0 is returned, then the timer is currently disabled.\r | |
eb16e240 | 247 | \r |
18c97f53 | 248 | @retval EFI_SUCCESS The timer period was returned in TimerPeriod.\r |
249 | @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.\r | |
eb16e240 | 250 | \r |
18c97f53 | 251 | **/\r |
252 | EFI_STATUS\r | |
253 | EFIAPI\r | |
254 | TimerDriverGetTimerPeriod (\r | |
255 | IN EFI_TIMER_ARCH_PROTOCOL *This,\r | |
256 | OUT UINT64 *TimerPeriod\r | |
257 | )\r | |
eb16e240 | 258 | {\r |
259 | if (TimerPeriod == NULL) {\r | |
260 | return EFI_INVALID_PARAMETER;\r | |
261 | }\r | |
262 | \r | |
263 | *TimerPeriod = mTimerPeriod;\r | |
264 | \r | |
265 | return EFI_SUCCESS;\r | |
266 | }\r | |
267 | \r | |
18c97f53 | 268 | /**\r |
eb16e240 | 269 | \r |
18c97f53 | 270 | This function generates a soft timer interrupt. If the platform does not support soft\r |
271 | timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.\r | |
272 | If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()\r | |
273 | service, then a soft timer interrupt will be generated. If the timer interrupt is\r | |
274 | enabled when this service is called, then the registered handler will be invoked. The\r | |
275 | registered handler should not be able to distinguish a hardware-generated timer\r | |
eb16e240 | 276 | interrupt from a software-generated timer interrupt.\r |
277 | \r | |
eb16e240 | 278 | \r |
18c97f53 | 279 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r |
eb16e240 | 280 | \r |
18c97f53 | 281 | @retval EFI_SUCCESS The soft timer interrupt was generated.\r |
9349c2e6 | 282 | @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.\r |
eb16e240 | 283 | \r |
18c97f53 | 284 | **/\r |
285 | EFI_STATUS\r | |
286 | EFIAPI\r | |
287 | TimerDriverGenerateSoftInterrupt (\r | |
288 | IN EFI_TIMER_ARCH_PROTOCOL *This\r | |
289 | )\r | |
eb16e240 | 290 | {\r |
291 | EFI_STATUS Status;\r | |
292 | UINT16 IRQMask;\r | |
293 | EFI_TPL OriginalTPL;\r | |
5a702acd | 294 | \r |
eb16e240 | 295 | //\r |
296 | // If the timer interrupt is enabled, then the registered handler will be invoked.\r | |
297 | //\r | |
298 | Status = mLegacy8259->GetMask (mLegacy8259, NULL, NULL, &IRQMask, NULL);\r | |
299 | ASSERT_EFI_ERROR (Status);\r | |
300 | if ((IRQMask & 0x1) == 0) {\r | |
301 | //\r | |
302 | // Invoke the registered handler\r | |
303 | //\r | |
304 | OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r | |
305 | \r | |
24115e44 | 306 | if (mTimerNotifyFunction != NULL) {\r |
eb16e240 | 307 | //\r |
24115e44 | 308 | // @bug : This does not handle missed timer interrupts\r |
eb16e240 | 309 | //\r |
310 | mTimerNotifyFunction (mTimerPeriod);\r | |
311 | }\r | |
5a702acd | 312 | \r |
eb16e240 | 313 | gBS->RestoreTPL (OriginalTPL);\r |
314 | } else {\r | |
315 | return EFI_UNSUPPORTED;\r | |
316 | }\r | |
317 | \r | |
318 | return EFI_SUCCESS;\r | |
319 | }\r | |
320 | \r | |
18c97f53 | 321 | /**\r |
322 | Initialize the Timer Architectural Protocol driver\r | |
323 | \r | |
324 | @param ImageHandle ImageHandle of the loaded driver\r | |
325 | @param SystemTable Pointer to the System Table\r | |
326 | \r | |
327 | @retval EFI_SUCCESS Timer Architectural Protocol created\r | |
328 | @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.\r | |
619ad10f | 329 | @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.\r |
18c97f53 | 330 | \r |
331 | **/\r | |
eb16e240 | 332 | EFI_STATUS\r |
333 | EFIAPI\r | |
334 | TimerDriverInitialize (\r | |
335 | IN EFI_HANDLE ImageHandle,\r | |
336 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
337 | )\r | |
eb16e240 | 338 | {\r |
339 | EFI_STATUS Status;\r | |
340 | UINT32 TimerVector;\r | |
341 | \r | |
342 | //\r | |
343 | // Initialize the pointer to our notify function.\r | |
344 | //\r | |
345 | mTimerNotifyFunction = NULL;\r | |
346 | \r | |
347 | //\r | |
348 | // Make sure the Timer Architectural Protocol is not already installed in the system\r | |
349 | //\r | |
350 | ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiTimerArchProtocolGuid);\r | |
351 | \r | |
eb16e240 | 352 | //\r |
353 | // Find the CPU architectural protocol.\r | |
354 | //\r | |
355 | Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **) &mCpu);\r | |
356 | ASSERT_EFI_ERROR (Status);\r | |
357 | \r | |
358 | //\r | |
359 | // Find the Legacy8259 protocol.\r | |
360 | //\r | |
361 | Status = gBS->LocateProtocol (&gEfiLegacy8259ProtocolGuid, NULL, (VOID **) &mLegacy8259);\r | |
362 | ASSERT_EFI_ERROR (Status);\r | |
363 | \r | |
364 | //\r | |
365 | // Force the timer to be disabled\r | |
366 | //\r | |
367 | Status = TimerDriverSetTimerPeriod (&mTimer, 0);\r | |
368 | ASSERT_EFI_ERROR (Status);\r | |
369 | \r | |
370 | //\r | |
371 | // Get the interrupt vector number corresponding to IRQ0 from the 8259 driver\r | |
372 | //\r | |
373 | TimerVector = 0;\r | |
374 | Status = mLegacy8259->GetVector (mLegacy8259, Efi8259Irq0, (UINT8 *) &TimerVector);\r | |
375 | ASSERT_EFI_ERROR (Status);\r | |
376 | \r | |
377 | //\r | |
378 | // Install interrupt handler for 8254 Timer #0 (ISA IRQ0)\r | |
379 | //\r | |
380 | Status = mCpu->RegisterInterruptHandler (mCpu, TimerVector, TimerInterruptHandler);\r | |
381 | ASSERT_EFI_ERROR (Status);\r | |
382 | \r | |
383 | //\r | |
384 | // Force the timer to be enabled at its default period\r | |
385 | //\r | |
386 | Status = TimerDriverSetTimerPeriod (&mTimer, DEFAULT_TIMER_TICK_DURATION);\r | |
387 | ASSERT_EFI_ERROR (Status);\r | |
388 | \r | |
389 | //\r | |
390 | // Install the Timer Architectural Protocol onto a new handle\r | |
391 | //\r | |
392 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
393 | &mTimerHandle,\r | |
2952f72d | 394 | &gEfiTimerArchProtocolGuid, &mTimer,\r |
eb16e240 | 395 | NULL\r |
396 | );\r | |
397 | ASSERT_EFI_ERROR (Status);\r | |
398 | \r | |
399 | return Status;\r | |
400 | }\r | |
2952f72d | 401 | \r |