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