3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 NT Emulation Timer Architectural Protocol Driver as defined in DXE CIS
20 This Timer module uses an NT Thread to simulate the timer-tick driven
21 timer service. In the future, the Thread creation should possibly be
22 abstracted by the CPU architectural protocol
29 // Pointer to the CPU Architectural Protocol instance
31 EFI_CPU_ARCH_PROTOCOL
*mCpu
;
34 // The Timer Architectural Protocol that this driver produces
36 EFI_TIMER_ARCH_PROTOCOL mTimer
= {
37 WinNtTimerDriverRegisterHandler
,
38 WinNtTimerDriverSetTimerPeriod
,
39 WinNtTimerDriverGetTimerPeriod
,
40 WinNtTimerDriverGenerateSoftInterrupt
44 // Define a global that we can use to shut down the NT timer thread when
45 // the timer is canceled.
47 BOOLEAN mCancelTimerThread
= FALSE
;
50 // The notification function to call on every timer interrupt
52 EFI_TIMER_NOTIFY mTimerNotifyFunction
= NULL
;
55 // The current period of the timer interrupt
60 // The thread handle for this driver
62 HANDLE mNtMainThreadHandle
;
65 // The timer value from the last timer interrupt
70 // Critical section used to update varibles shared between the main thread and
71 // the timer interrupt thread.
73 CRITICAL_SECTION mNtCriticalSection
;
78 UINT mMMTimerThreadID
= 0;
93 TODO: Add function description
97 wTimerID - TODO: add argument description
98 msg - TODO: add argument description
99 dwUser - TODO: add argument description
100 dw1 - TODO: add argument description
101 dw2 - TODO: add argument description
105 TODO: add return values
112 EFI_TIMER_NOTIFY CallbackFunction
;
113 BOOLEAN InterruptState
;
115 if (!mCancelTimerThread
) {
118 // Suspend the main thread until we are done
121 gWinNt
->SuspendThread (mNtMainThreadHandle
);
124 // If the timer thread is being canceled, then bail immediately.
125 // We check again here because there's a small window of time from when
126 // this thread was kicked off and when we suspended the main thread above.
128 if (mCancelTimerThread
) {
129 gWinNt
->ResumeThread (mNtMainThreadHandle
);
130 gWinNt
->timeKillEvent (wTimerID
);
131 mMMTimerThreadID
= 0;
135 mCpu
->GetInterruptState (mCpu
, &InterruptState
);
136 while (!InterruptState
) {
138 // Resume the main thread
140 gWinNt
->ResumeThread (mNtMainThreadHandle
);
143 // Wait for interrupts to be enabled.
145 mCpu
->GetInterruptState (mCpu
, &InterruptState
);
146 while (!InterruptState
) {
148 mCpu
->GetInterruptState (mCpu
, &InterruptState
);
152 // Suspend the main thread until we are done
154 gWinNt
->SuspendThread (mNtMainThreadHandle
);
155 mCpu
->GetInterruptState (mCpu
, &InterruptState
);
159 // Get the current system tick
161 CurrentTick
= gWinNt
->GetTickCount ();
162 Delta
= CurrentTick
- mNtLastTick
;
163 mNtLastTick
= CurrentTick
;
166 // If delay was more then 1 second, ignore it (probably debugging case)
170 OriginalTPL
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
173 // Inform the firmware of an "timer interrupt". The time
174 // expired since the last call is 10,000 times the number
175 // of ms. (or 100ns units)
177 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
178 CallbackFunction
= mTimerNotifyFunction
;
179 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
182 // Only invoke the callback function if a Non-NULL handler has been
183 // registered. Assume all other handlers are legal.
185 if (CallbackFunction
!= NULL
) {
186 CallbackFunction ((UINT64
) (Delta
* 10000));
189 gBS
->RestoreTPL (OriginalTPL
);
194 // Resume the main thread
196 gWinNt
->ResumeThread (mNtMainThreadHandle
);
198 gWinNt
->timeKillEvent (wTimerID
);
199 mMMTimerThreadID
= 0;
212 It is used to emulate a platform
213 timer-driver interrupt handler.
220 // TODO: function comment is missing 'Arguments:'
225 // Set our thread priority higher than the "main" thread.
227 gWinNt
->SetThreadPriority (
228 gWinNt
->GetCurrentThread (),
229 THREAD_PRIORITY_HIGHEST
233 // Calc the appropriate interval
235 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
236 SleepCount
= (UINT32
) (mTimerPeriod
+ 5000) / 10000;
237 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
239 return gWinNt
->timeSetEvent (
244 TIME_PERIODIC
| TIME_KILL_SYNCHRONOUS
| TIME_CALLBACK_FUNCTION
251 WinNtTimerDriverRegisterHandler (
252 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
253 IN EFI_TIMER_NOTIFY NotifyFunction
259 This function registers the handler NotifyFunction so it is called every time
260 the timer interrupt fires. It also passes the amount of time since the last
261 handler call to the NotifyFunction. If NotifyFunction is NULL, then the
262 handler is unregistered. If the handler is registered, then EFI_SUCCESS is
263 returned. If the CPU does not support registering a timer interrupt handler,
264 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
265 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
266 If an attempt is made to unregister a handler when a handler is not registered,
267 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
268 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
273 This - The EFI_TIMER_ARCH_PROTOCOL instance.
275 NotifyFunction - The function to call when a timer interrupt fires. This
276 function executes at TPL_HIGH_LEVEL. The DXE Core will
277 register a handler for the timer interrupt, so it can know
278 how much time has passed. This information is used to
279 signal timer based events. NULL will unregister the handler.
283 EFI_SUCCESS - The timer handler was registered.
285 EFI_UNSUPPORTED - The platform does not support timer interrupts.
287 EFI_ALREADY_STARTED - NotifyFunction is not NULL, and a handler is already
290 EFI_INVALID_PARAMETER - NotifyFunction is NULL, and a handler was not
291 previously registered.
293 EFI_DEVICE_ERROR - The timer handler could not be registered.
298 // Check for invalid parameters
300 if (NotifyFunction
== NULL
&& mTimerNotifyFunction
== NULL
) {
301 return EFI_INVALID_PARAMETER
;
304 if (NotifyFunction
!= NULL
&& mTimerNotifyFunction
!= NULL
) {
305 return EFI_ALREADY_STARTED
;
309 // Use Critical Section to update the notification function that is
310 // used from the timer interrupt thread.
312 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
314 mTimerNotifyFunction
= NotifyFunction
;
316 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
323 WinNtTimerDriverSetTimerPeriod (
324 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
325 IN UINT64 TimerPeriod
331 This function adjusts the period of timer interrupts to the value specified
332 by TimerPeriod. If the timer period is updated, then the selected timer
333 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
334 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
335 If an error occurs while attempting to update the timer period, then the
336 timer hardware will be put back in its state prior to this call, and
337 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
338 is disabled. This is not the same as disabling the CPU's interrupts.
339 Instead, it must either turn off the timer hardware, or it must adjust the
340 interrupt controller so that a CPU interrupt is not generated when the timer
345 This - The EFI_TIMER_ARCH_PROTOCOL instance.
347 TimerPeriod - The rate to program the timer interrupt in 100 nS units. If
348 the timer hardware is not programmable, then EFI_UNSUPPORTED is
349 returned. If the timer is programmable, then the timer period
350 will be rounded up to the nearest timer period that is supported
351 by the timer hardware. If TimerPeriod is set to 0, then the
352 timer interrupts will be disabled.
356 EFI_SUCCESS - The timer period was changed.
358 EFI_UNSUPPORTED - The platform cannot change the period of the timer interrupt.
360 EFI_DEVICE_ERROR - The timer period could not be changed due to a device error.
366 // If TimerPeriod is 0, then the timer thread should be canceled
368 if (TimerPeriod
== 0) {
370 // Cancel the timer thread
372 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
374 mCancelTimerThread
= TRUE
;
376 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
379 // Wait for the timer thread to exit
382 if (mMMTimerThreadID
) {
383 gWinNt
->timeKillEvent (mMMTimerThreadID
);
386 mMMTimerThreadID
= 0;
389 // Update the timer period
391 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
393 mTimerPeriod
= TimerPeriod
;
395 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
398 // NULL out the thread handle so it will be re-created if the timer is enabled again
401 } else if ((TimerPeriod
> TIMER_MINIMUM_VALUE
) && (TimerPeriod
< TIMER_MAXIMUM_VALUE
)) {
403 // If the TimerPeriod is valid, then create and/or adjust the period of the timer thread
405 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
407 mTimerPeriod
= TimerPeriod
;
409 mCancelTimerThread
= FALSE
;
411 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
414 // Get the starting tick location if we are just starting the timer thread
416 mNtLastTick
= gWinNt
->GetTickCount ();
418 if (mMMTimerThreadID
) {
419 gWinNt
->timeKillEvent (mMMTimerThreadID
);
422 mMMTimerThreadID
= 0;
424 mMMTimerThreadID
= CreateNtTimer ();
433 WinNtTimerDriverGetTimerPeriod (
434 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
435 OUT UINT64
*TimerPeriod
441 This function retrieves the period of timer interrupts in 100 ns units,
442 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
443 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
444 returned, then the timer is currently disabled.
448 This - The EFI_TIMER_ARCH_PROTOCOL instance.
450 TimerPeriod - A pointer to the timer period to retrieve in 100 ns units. If
451 0 is returned, then the timer is currently disabled.
455 EFI_SUCCESS - The timer period was returned in TimerPeriod.
457 EFI_INVALID_PARAMETER - TimerPeriod is NULL.
461 if (TimerPeriod
== NULL
) {
462 return EFI_INVALID_PARAMETER
;
465 *TimerPeriod
= mTimerPeriod
;
472 WinNtTimerDriverGenerateSoftInterrupt (
473 IN EFI_TIMER_ARCH_PROTOCOL
*This
479 This function generates a soft timer interrupt. If the platform does not support soft
480 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
481 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
482 service, then a soft timer interrupt will be generated. If the timer interrupt is
483 enabled when this service is called, then the registered handler will be invoked. The
484 registered handler should not be able to distinguish a hardware-generated timer
485 interrupt from a software-generated timer interrupt.
489 This - The EFI_TIMER_ARCH_PROTOCOL instance.
493 EFI_SUCCESS - The soft timer interrupt was generated.
495 EFI_UNSUPPORTEDT - The platform does not support the generation of soft timer interrupts.
499 return EFI_UNSUPPORTED
;
505 WinNtTimerDriverInitialize (
506 IN EFI_HANDLE ImageHandle
,
507 IN EFI_SYSTEM_TABLE
*SystemTable
513 Initialize the Timer Architectural Protocol driver
517 ImageHandle - ImageHandle of the loaded driver
519 SystemTable - Pointer to the System Table
523 EFI_SUCCESS - Timer Architectural Protocol created
525 EFI_OUT_OF_RESOURCES - Not enough resources available to initialize driver.
527 EFI_DEVICE_ERROR - A device error occured attempting to initialize the driver.
536 // Make sure the Timer Architectural Protocol is not already installed in the system
538 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiTimerArchProtocolGuid
);
541 // Get the CPU Architectural Protocol instance
543 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, &mCpu
);
544 ASSERT_EFI_ERROR (Status
);
547 // Get our handle so the timer tick thread can suspend
549 Result
= gWinNt
->DuplicateHandle (
550 gWinNt
->GetCurrentProcess (),
551 gWinNt
->GetCurrentThread (),
552 gWinNt
->GetCurrentProcess (),
553 &mNtMainThreadHandle
,
556 DUPLICATE_SAME_ACCESS
559 return EFI_DEVICE_ERROR
;
563 // Initialize Critical Section used to update variables shared between the main
564 // thread and the timer interrupt thread.
566 gWinNt
->InitializeCriticalSection (&mNtCriticalSection
);
569 // Start the timer thread at the default timer period
571 Status
= mTimer
.SetTimerPeriod (&mTimer
, DEFAULT_TIMER_TICK_DURATION
);
572 if (EFI_ERROR (Status
)) {
573 gWinNt
->DeleteCriticalSection (&mNtCriticalSection
);
578 // Install the Timer Architectural Protocol onto a new handle
581 Status
= gBS
->InstallProtocolInterface (
583 &gEfiTimerArchProtocolGuid
,
584 EFI_NATIVE_INTERFACE
,
587 if (EFI_ERROR (Status
)) {
591 mTimer
.SetTimerPeriod (&mTimer
, 0);
592 gWinNt
->DeleteCriticalSection (&mNtCriticalSection
);