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.
119 // Enter the critical section before suspending
120 // and leave the critical section after resuming
121 // to avoid deadlock between main and timer thread.
123 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
124 gWinNt
->SuspendThread (mNtMainThreadHandle
);
127 // If the timer thread is being canceled, then bail immediately.
128 // We check again here because there's a small window of time from when
129 // this thread was kicked off and when we suspended the main thread above.
131 if (mCancelTimerThread
) {
132 gWinNt
->ResumeThread (mNtMainThreadHandle
);
133 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
134 gWinNt
->timeKillEvent (wTimerID
);
135 mMMTimerThreadID
= 0;
139 mCpu
->GetInterruptState (mCpu
, &InterruptState
);
140 while (!InterruptState
) {
142 // Resume the main thread
144 gWinNt
->ResumeThread (mNtMainThreadHandle
);
145 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
148 // Wait for interrupts to be enabled.
150 mCpu
->GetInterruptState (mCpu
, &InterruptState
);
151 while (!InterruptState
) {
153 mCpu
->GetInterruptState (mCpu
, &InterruptState
);
157 // Suspend the main thread until we are done
159 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
160 gWinNt
->SuspendThread (mNtMainThreadHandle
);
161 mCpu
->GetInterruptState (mCpu
, &InterruptState
);
165 // Get the current system tick
167 CurrentTick
= gWinNt
->GetTickCount ();
168 Delta
= CurrentTick
- mNtLastTick
;
169 mNtLastTick
= CurrentTick
;
172 // If delay was more then 1 second, ignore it (probably debugging case)
176 OriginalTPL
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
179 // Inform the firmware of an "timer interrupt". The time
180 // expired since the last call is 10,000 times the number
181 // of ms. (or 100ns units)
183 CallbackFunction
= mTimerNotifyFunction
;
186 // Only invoke the callback function if a Non-NULL handler has been
187 // registered. Assume all other handlers are legal.
189 if (CallbackFunction
!= NULL
) {
190 CallbackFunction ((UINT64
) (Delta
* 10000));
193 gBS
->RestoreTPL (OriginalTPL
);
198 // Resume the main thread
200 gWinNt
->ResumeThread (mNtMainThreadHandle
);
201 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
203 gWinNt
->timeKillEvent (wTimerID
);
204 mMMTimerThreadID
= 0;
217 It is used to emulate a platform
218 timer-driver interrupt handler.
225 // TODO: function comment is missing 'Arguments:'
230 // Set our thread priority higher than the "main" thread.
232 gWinNt
->SetThreadPriority (
233 gWinNt
->GetCurrentThread (),
234 THREAD_PRIORITY_HIGHEST
238 // Calc the appropriate interval
240 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
241 SleepCount
= (UINT32
) (mTimerPeriod
+ 5000) / 10000;
242 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
244 return gWinNt
->timeSetEvent (
249 TIME_PERIODIC
| TIME_KILL_SYNCHRONOUS
| TIME_CALLBACK_FUNCTION
256 WinNtTimerDriverRegisterHandler (
257 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
258 IN EFI_TIMER_NOTIFY NotifyFunction
264 This function registers the handler NotifyFunction so it is called every time
265 the timer interrupt fires. It also passes the amount of time since the last
266 handler call to the NotifyFunction. If NotifyFunction is NULL, then the
267 handler is unregistered. If the handler is registered, then EFI_SUCCESS is
268 returned. If the CPU does not support registering a timer interrupt handler,
269 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
270 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
271 If an attempt is made to unregister a handler when a handler is not registered,
272 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
273 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
278 This - The EFI_TIMER_ARCH_PROTOCOL instance.
280 NotifyFunction - The function to call when a timer interrupt fires. This
281 function executes at TPL_HIGH_LEVEL. The DXE Core will
282 register a handler for the timer interrupt, so it can know
283 how much time has passed. This information is used to
284 signal timer based events. NULL will unregister the handler.
288 EFI_SUCCESS - The timer handler was registered.
290 EFI_UNSUPPORTED - The platform does not support timer interrupts.
292 EFI_ALREADY_STARTED - NotifyFunction is not NULL, and a handler is already
295 EFI_INVALID_PARAMETER - NotifyFunction is NULL, and a handler was not
296 previously registered.
298 EFI_DEVICE_ERROR - The timer handler could not be registered.
303 // Check for invalid parameters
305 if (NotifyFunction
== NULL
&& mTimerNotifyFunction
== NULL
) {
306 return EFI_INVALID_PARAMETER
;
309 if (NotifyFunction
!= NULL
&& mTimerNotifyFunction
!= NULL
) {
310 return EFI_ALREADY_STARTED
;
314 // Use Critical Section to update the notification function that is
315 // used from the timer interrupt thread.
317 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
319 mTimerNotifyFunction
= NotifyFunction
;
321 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
328 WinNtTimerDriverSetTimerPeriod (
329 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
330 IN UINT64 TimerPeriod
336 This function adjusts the period of timer interrupts to the value specified
337 by TimerPeriod. If the timer period is updated, then the selected timer
338 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
339 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
340 If an error occurs while attempting to update the timer period, then the
341 timer hardware will be put back in its state prior to this call, and
342 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
343 is disabled. This is not the same as disabling the CPU's interrupts.
344 Instead, it must either turn off the timer hardware, or it must adjust the
345 interrupt controller so that a CPU interrupt is not generated when the timer
350 This - The EFI_TIMER_ARCH_PROTOCOL instance.
352 TimerPeriod - The rate to program the timer interrupt in 100 nS units. If
353 the timer hardware is not programmable, then EFI_UNSUPPORTED is
354 returned. If the timer is programmable, then the timer period
355 will be rounded up to the nearest timer period that is supported
356 by the timer hardware. If TimerPeriod is set to 0, then the
357 timer interrupts will be disabled.
361 EFI_SUCCESS - The timer period was changed.
363 EFI_UNSUPPORTED - The platform cannot change the period of the timer interrupt.
365 EFI_DEVICE_ERROR - The timer period could not be changed due to a device error.
371 // If TimerPeriod is 0, then the timer thread should be canceled
373 if (TimerPeriod
== 0) {
375 // Cancel the timer thread
377 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
379 mCancelTimerThread
= TRUE
;
381 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
384 // Wait for the timer thread to exit
387 if (mMMTimerThreadID
) {
388 gWinNt
->timeKillEvent (mMMTimerThreadID
);
391 mMMTimerThreadID
= 0;
394 // Update the timer period
396 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
398 mTimerPeriod
= TimerPeriod
;
400 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
403 // NULL out the thread handle so it will be re-created if the timer is enabled again
406 } else if ((TimerPeriod
> TIMER_MINIMUM_VALUE
) && (TimerPeriod
< TIMER_MAXIMUM_VALUE
)) {
408 // If the TimerPeriod is valid, then create and/or adjust the period of the timer thread
410 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
412 mTimerPeriod
= TimerPeriod
;
414 mCancelTimerThread
= FALSE
;
416 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
419 // Get the starting tick location if we are just starting the timer thread
421 mNtLastTick
= gWinNt
->GetTickCount ();
423 if (mMMTimerThreadID
) {
424 gWinNt
->timeKillEvent (mMMTimerThreadID
);
427 mMMTimerThreadID
= 0;
429 mMMTimerThreadID
= CreateNtTimer ();
438 WinNtTimerDriverGetTimerPeriod (
439 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
440 OUT UINT64
*TimerPeriod
446 This function retrieves the period of timer interrupts in 100 ns units,
447 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
448 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
449 returned, then the timer is currently disabled.
453 This - The EFI_TIMER_ARCH_PROTOCOL instance.
455 TimerPeriod - A pointer to the timer period to retrieve in 100 ns units. If
456 0 is returned, then the timer is currently disabled.
460 EFI_SUCCESS - The timer period was returned in TimerPeriod.
462 EFI_INVALID_PARAMETER - TimerPeriod is NULL.
466 if (TimerPeriod
== NULL
) {
467 return EFI_INVALID_PARAMETER
;
470 *TimerPeriod
= mTimerPeriod
;
477 WinNtTimerDriverGenerateSoftInterrupt (
478 IN EFI_TIMER_ARCH_PROTOCOL
*This
484 This function generates a soft timer interrupt. If the platform does not support soft
485 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
486 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
487 service, then a soft timer interrupt will be generated. If the timer interrupt is
488 enabled when this service is called, then the registered handler will be invoked. The
489 registered handler should not be able to distinguish a hardware-generated timer
490 interrupt from a software-generated timer interrupt.
494 This - The EFI_TIMER_ARCH_PROTOCOL instance.
498 EFI_SUCCESS - The soft timer interrupt was generated.
500 EFI_UNSUPPORTEDT - The platform does not support the generation of soft timer interrupts.
504 return EFI_UNSUPPORTED
;
510 WinNtTimerDriverInitialize (
511 IN EFI_HANDLE ImageHandle
,
512 IN EFI_SYSTEM_TABLE
*SystemTable
518 Initialize the Timer Architectural Protocol driver
522 ImageHandle - ImageHandle of the loaded driver
524 SystemTable - Pointer to the System Table
528 EFI_SUCCESS - Timer Architectural Protocol created
530 EFI_OUT_OF_RESOURCES - Not enough resources available to initialize driver.
532 EFI_DEVICE_ERROR - A device error occured attempting to initialize the driver.
539 EFI_HANDLE hSourceProcessHandle
;
540 EFI_HANDLE hSourceHandle
;
541 EFI_HANDLE hTargetProcessHandle
;
543 // Make sure the Timer Architectural Protocol is not already installed in the system
545 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiTimerArchProtocolGuid
);
548 // Get the CPU Architectural Protocol instance
550 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&mCpu
);
551 ASSERT_EFI_ERROR (Status
);
554 // Get our handle so the timer tick thread can suspend
556 hSourceProcessHandle
= gWinNt
->GetCurrentProcess ();
557 hSourceHandle
= gWinNt
->GetCurrentThread ();
558 hTargetProcessHandle
= gWinNt
->GetCurrentProcess ();
559 Result
= gWinNt
->DuplicateHandle (
560 hSourceProcessHandle
,
562 hTargetProcessHandle
,
563 &mNtMainThreadHandle
,
566 DUPLICATE_SAME_ACCESS
569 return EFI_DEVICE_ERROR
;
573 // Initialize Critical Section used to update variables shared between the main
574 // thread and the timer interrupt thread.
576 gWinNt
->InitializeCriticalSection (&mNtCriticalSection
);
579 // Start the timer thread at the default timer period
581 Status
= mTimer
.SetTimerPeriod (&mTimer
, DEFAULT_TIMER_TICK_DURATION
);
582 if (EFI_ERROR (Status
)) {
583 gWinNt
->DeleteCriticalSection (&mNtCriticalSection
);
588 // Install the Timer Architectural Protocol onto a new handle
591 Status
= gBS
->InstallProtocolInterface (
593 &gEfiTimerArchProtocolGuid
,
594 EFI_NATIVE_INTERFACE
,
597 if (EFI_ERROR (Status
)) {
601 mTimer
.SetTimerPeriod (&mTimer
, 0);
602 gWinNt
->DeleteCriticalSection (&mNtCriticalSection
);