3 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
12 NT Emulation Timer Architectural Protocol Driver as defined in DXE CIS
14 This Timer module uses an NT Thread to simulate the timer-tick driven
15 timer service. In the future, the Thread creation should possibly be
16 abstracted by the CPU architectural protocol
23 // Pointer to the CPU Architectural Protocol instance
25 EFI_CPU_ARCH_PROTOCOL
*mCpu
;
28 // The Timer Architectural Protocol that this driver produces
30 EFI_TIMER_ARCH_PROTOCOL mTimer
= {
31 WinNtTimerDriverRegisterHandler
,
32 WinNtTimerDriverSetTimerPeriod
,
33 WinNtTimerDriverGetTimerPeriod
,
34 WinNtTimerDriverGenerateSoftInterrupt
38 // Define a global that we can use to shut down the NT timer thread when
39 // the timer is canceled.
41 BOOLEAN mCancelTimerThread
= FALSE
;
44 // The notification function to call on every timer interrupt
46 EFI_TIMER_NOTIFY mTimerNotifyFunction
= NULL
;
49 // The current period of the timer interrupt
54 // The thread handle for this driver
56 HANDLE mNtMainThreadHandle
;
59 // The timer value from the last timer interrupt
64 // Critical section used to update varibles shared between the main thread and
65 // the timer interrupt thread.
67 CRITICAL_SECTION mNtCriticalSection
;
72 UINT mMMTimerThreadID
= 0;
87 TODO: Add function description
91 wTimerID - TODO: add argument description
92 msg - TODO: add argument description
93 dwUser - TODO: add argument description
94 dw1 - TODO: add argument description
95 dw2 - TODO: add argument description
99 TODO: add return values
106 EFI_TIMER_NOTIFY CallbackFunction
;
107 BOOLEAN InterruptState
;
109 if (!mCancelTimerThread
) {
112 // Suspend the main thread until we are done.
113 // Enter the critical section before suspending
114 // and leave the critical section after resuming
115 // to avoid deadlock between main and timer thread.
117 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
118 gWinNt
->SuspendThread (mNtMainThreadHandle
);
121 // If the timer thread is being canceled, then bail immediately.
122 // We check again here because there's a small window of time from when
123 // this thread was kicked off and when we suspended the main thread above.
125 if (mCancelTimerThread
) {
126 gWinNt
->ResumeThread (mNtMainThreadHandle
);
127 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
128 gWinNt
->timeKillEvent (wTimerID
);
129 mMMTimerThreadID
= 0;
133 mCpu
->GetInterruptState (mCpu
, &InterruptState
);
134 while (!InterruptState
) {
136 // Resume the main thread
138 gWinNt
->ResumeThread (mNtMainThreadHandle
);
139 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
142 // Wait for interrupts to be enabled.
144 mCpu
->GetInterruptState (mCpu
, &InterruptState
);
145 while (!InterruptState
) {
147 mCpu
->GetInterruptState (mCpu
, &InterruptState
);
151 // Suspend the main thread until we are done
153 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
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 CallbackFunction
= mTimerNotifyFunction
;
180 // Only invoke the callback function if a Non-NULL handler has been
181 // registered. Assume all other handlers are legal.
183 if (CallbackFunction
!= NULL
) {
184 CallbackFunction ((UINT64
) (Delta
* 10000));
187 gBS
->RestoreTPL (OriginalTPL
);
192 // Resume the main thread
194 gWinNt
->ResumeThread (mNtMainThreadHandle
);
195 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
197 gWinNt
->timeKillEvent (wTimerID
);
198 mMMTimerThreadID
= 0;
211 It is used to emulate a platform
212 timer-driver interrupt handler.
219 // TODO: function comment is missing 'Arguments:'
224 // Set our thread priority higher than the "main" thread.
226 gWinNt
->SetThreadPriority (
227 gWinNt
->GetCurrentThread (),
228 THREAD_PRIORITY_HIGHEST
232 // Calc the appropriate interval
234 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
235 SleepCount
= (UINT32
) (mTimerPeriod
+ 5000) / 10000;
236 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
238 return gWinNt
->timeSetEvent (
243 TIME_PERIODIC
| TIME_KILL_SYNCHRONOUS
| TIME_CALLBACK_FUNCTION
250 WinNtTimerDriverRegisterHandler (
251 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
252 IN EFI_TIMER_NOTIFY NotifyFunction
258 This function registers the handler NotifyFunction so it is called every time
259 the timer interrupt fires. It also passes the amount of time since the last
260 handler call to the NotifyFunction. If NotifyFunction is NULL, then the
261 handler is unregistered. If the handler is registered, then EFI_SUCCESS is
262 returned. If the CPU does not support registering a timer interrupt handler,
263 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
264 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
265 If an attempt is made to unregister a handler when a handler is not registered,
266 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
267 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
272 This - The EFI_TIMER_ARCH_PROTOCOL instance.
274 NotifyFunction - The function to call when a timer interrupt fires. This
275 function executes at TPL_HIGH_LEVEL. The DXE Core will
276 register a handler for the timer interrupt, so it can know
277 how much time has passed. This information is used to
278 signal timer based events. NULL will unregister the handler.
282 EFI_SUCCESS - The timer handler was registered.
284 EFI_UNSUPPORTED - The platform does not support timer interrupts.
286 EFI_ALREADY_STARTED - NotifyFunction is not NULL, and a handler is already
289 EFI_INVALID_PARAMETER - NotifyFunction is NULL, and a handler was not
290 previously registered.
292 EFI_DEVICE_ERROR - The timer handler could not be registered.
297 // Check for invalid parameters
299 if (NotifyFunction
== NULL
&& mTimerNotifyFunction
== NULL
) {
300 return EFI_INVALID_PARAMETER
;
303 if (NotifyFunction
!= NULL
&& mTimerNotifyFunction
!= NULL
) {
304 return EFI_ALREADY_STARTED
;
308 // Use Critical Section to update the notification function that is
309 // used from the timer interrupt thread.
311 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
313 mTimerNotifyFunction
= NotifyFunction
;
315 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
322 WinNtTimerDriverSetTimerPeriod (
323 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
324 IN UINT64 TimerPeriod
330 This function adjusts the period of timer interrupts to the value specified
331 by TimerPeriod. If the timer period is updated, then the selected timer
332 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
333 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
334 If an error occurs while attempting to update the timer period, then the
335 timer hardware will be put back in its state prior to this call, and
336 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
337 is disabled. This is not the same as disabling the CPU's interrupts.
338 Instead, it must either turn off the timer hardware, or it must adjust the
339 interrupt controller so that a CPU interrupt is not generated when the timer
344 This - The EFI_TIMER_ARCH_PROTOCOL instance.
346 TimerPeriod - The rate to program the timer interrupt in 100 nS units. If
347 the timer hardware is not programmable, then EFI_UNSUPPORTED is
348 returned. If the timer is programmable, then the timer period
349 will be rounded up to the nearest timer period that is supported
350 by the timer hardware. If TimerPeriod is set to 0, then the
351 timer interrupts will be disabled.
355 EFI_SUCCESS - The timer period was changed.
357 EFI_UNSUPPORTED - The platform cannot change the period of the timer interrupt.
359 EFI_DEVICE_ERROR - The timer period could not be changed due to a device error.
365 // If TimerPeriod is 0, then the timer thread should be canceled
367 if (TimerPeriod
== 0) {
369 // Cancel the timer thread
371 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
373 mCancelTimerThread
= TRUE
;
375 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
378 // Wait for the timer thread to exit
381 if (mMMTimerThreadID
) {
382 gWinNt
->timeKillEvent (mMMTimerThreadID
);
385 mMMTimerThreadID
= 0;
388 // Update the timer period
390 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
392 mTimerPeriod
= TimerPeriod
;
394 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
397 // NULL out the thread handle so it will be re-created if the timer is enabled again
400 } else if ((TimerPeriod
> TIMER_MINIMUM_VALUE
) && (TimerPeriod
< TIMER_MAXIMUM_VALUE
)) {
402 // If the TimerPeriod is valid, then create and/or adjust the period of the timer thread
404 gWinNt
->EnterCriticalSection (&mNtCriticalSection
);
406 mTimerPeriod
= TimerPeriod
;
408 mCancelTimerThread
= FALSE
;
410 gWinNt
->LeaveCriticalSection (&mNtCriticalSection
);
413 // Get the starting tick location if we are just starting the timer thread
415 mNtLastTick
= gWinNt
->GetTickCount ();
417 if (mMMTimerThreadID
) {
418 gWinNt
->timeKillEvent (mMMTimerThreadID
);
421 mMMTimerThreadID
= 0;
423 mMMTimerThreadID
= CreateNtTimer ();
432 WinNtTimerDriverGetTimerPeriod (
433 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
434 OUT UINT64
*TimerPeriod
440 This function retrieves the period of timer interrupts in 100 ns units,
441 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
442 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
443 returned, then the timer is currently disabled.
447 This - The EFI_TIMER_ARCH_PROTOCOL instance.
449 TimerPeriod - A pointer to the timer period to retrieve in 100 ns units. If
450 0 is returned, then the timer is currently disabled.
454 EFI_SUCCESS - The timer period was returned in TimerPeriod.
456 EFI_INVALID_PARAMETER - TimerPeriod is NULL.
460 if (TimerPeriod
== NULL
) {
461 return EFI_INVALID_PARAMETER
;
464 *TimerPeriod
= mTimerPeriod
;
471 WinNtTimerDriverGenerateSoftInterrupt (
472 IN EFI_TIMER_ARCH_PROTOCOL
*This
478 This function generates a soft timer interrupt. If the platform does not support soft
479 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
480 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
481 service, then a soft timer interrupt will be generated. If the timer interrupt is
482 enabled when this service is called, then the registered handler will be invoked. The
483 registered handler should not be able to distinguish a hardware-generated timer
484 interrupt from a software-generated timer interrupt.
488 This - The EFI_TIMER_ARCH_PROTOCOL instance.
492 EFI_SUCCESS - The soft timer interrupt was generated.
494 EFI_UNSUPPORTED - The platform does not support the generation of soft timer interrupts.
498 return EFI_UNSUPPORTED
;
504 WinNtTimerDriverInitialize (
505 IN EFI_HANDLE ImageHandle
,
506 IN EFI_SYSTEM_TABLE
*SystemTable
512 Initialize the Timer Architectural Protocol driver
516 ImageHandle - ImageHandle of the loaded driver
518 SystemTable - Pointer to the System Table
522 EFI_SUCCESS - Timer Architectural Protocol created
524 EFI_OUT_OF_RESOURCES - Not enough resources available to initialize driver.
526 EFI_DEVICE_ERROR - A device error occured attempting to initialize the driver.
533 EFI_HANDLE hSourceProcessHandle
;
534 EFI_HANDLE hSourceHandle
;
535 EFI_HANDLE hTargetProcessHandle
;
537 // Make sure the Timer Architectural Protocol is not already installed in the system
539 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiTimerArchProtocolGuid
);
542 // Get the CPU Architectural Protocol instance
544 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&mCpu
);
545 ASSERT_EFI_ERROR (Status
);
548 // Get our handle so the timer tick thread can suspend
550 hSourceProcessHandle
= gWinNt
->GetCurrentProcess ();
551 hSourceHandle
= gWinNt
->GetCurrentThread ();
552 hTargetProcessHandle
= gWinNt
->GetCurrentProcess ();
553 Result
= gWinNt
->DuplicateHandle (
554 hSourceProcessHandle
,
556 hTargetProcessHandle
,
557 &mNtMainThreadHandle
,
560 DUPLICATE_SAME_ACCESS
563 return EFI_DEVICE_ERROR
;
567 // Initialize Critical Section used to update variables shared between the main
568 // thread and the timer interrupt thread.
570 gWinNt
->InitializeCriticalSection (&mNtCriticalSection
);
573 // Start the timer thread at the default timer period
575 Status
= mTimer
.SetTimerPeriod (&mTimer
, DEFAULT_TIMER_TICK_DURATION
);
576 if (EFI_ERROR (Status
)) {
577 gWinNt
->DeleteCriticalSection (&mNtCriticalSection
);
582 // Install the Timer Architectural Protocol onto a new handle
585 Status
= gBS
->InstallProtocolInterface (
587 &gEfiTimerArchProtocolGuid
,
588 EFI_NATIVE_INTERFACE
,
591 if (EFI_ERROR (Status
)) {
595 mTimer
.SetTimerPeriod (&mTimer
, 0);
596 gWinNt
->DeleteCriticalSection (&mNtCriticalSection
);