timer.c
来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 605 行 · 第 1/2 页
C
605 行
/*++
Copyright (c) 2004, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
Module Name:
Timer.c
Abstract:
NT Emulation Timer Architectural Protocol Driver as defined in DXE CIS
This Timer module uses an NT Thread to simulate the timer-tick driven
timer service. In the future, the Thread creation should possibly be
abstracted by the CPU architectural protocol
--*/
#include "Timer.h"
//
// Pointer to the CPU Architectural Protocol instance
//
EFI_CPU_ARCH_PROTOCOL *mCpu;
//
// The Timer Architectural Protocol that this driver produces
//
EFI_TIMER_ARCH_PROTOCOL mTimer = {
WinNtTimerDriverRegisterHandler,
WinNtTimerDriverSetTimerPeriod,
WinNtTimerDriverGetTimerPeriod,
WinNtTimerDriverGenerateSoftInterrupt
};
//
// Define a global that we can use to shut down the NT timer thread when
// the timer is canceled.
//
BOOLEAN mCancelTimerThread = FALSE;
//
// The notification function to call on every timer interrupt
//
EFI_TIMER_NOTIFY mTimerNotifyFunction = NULL;
//
// The current period of the timer interrupt
//
UINT64 mTimerPeriod;
//
// The thread handle for this driver
//
HANDLE mNtMainThreadHandle;
//
// The timer value from the last timer interrupt
//
UINT32 mNtLastTick;
//
// Critical section used to update varibles shared between the main thread and
// the timer interrupt thread.
//
CRITICAL_SECTION mNtCriticalSection;
//
// Worker Functions
//
UINT mMMTimerThreadID = 0;
VOID
CALLBACK
MMTimerThread (
UINT wTimerID,
UINT msg,
DWORD dwUser,
DWORD dw1,
DWORD dw2
)
/*++
Routine Description:
TODO: Add function description
Arguments:
wTimerID - TODO: add argument description
msg - TODO: add argument description
dwUser - TODO: add argument description
dw1 - TODO: add argument description
dw2 - TODO: add argument description
Returns:
TODO: add return values
--*/
{
EFI_TPL OriginalTPL;
UINT32 CurrentTick;
UINT32 Delta;
EFI_TIMER_NOTIFY CallbackFunction;
BOOLEAN InterruptState;
if (!mCancelTimerThread) {
//
// Suspend the main thread until we are done
//
gWinNt->SuspendThread (mNtMainThreadHandle);
//
// If the timer thread is being canceled, then bail immediately.
// We check again here because there's a small window of time from when
// this thread was kicked off and when we suspended the main thread above.
//
if (mCancelTimerThread) {
gWinNt->ResumeThread (mNtMainThreadHandle);
gWinNt->timeKillEvent (wTimerID);
mMMTimerThreadID = 0;
return ;
}
mCpu->GetInterruptState (mCpu, &InterruptState);
while (!InterruptState) {
//
// Resume the main thread
//
gWinNt->ResumeThread (mNtMainThreadHandle);
//
// Wait for interrupts to be enabled.
//
mCpu->GetInterruptState (mCpu, &InterruptState);
while (!InterruptState) {
gWinNt->Sleep (0);
mCpu->GetInterruptState (mCpu, &InterruptState);
}
//
// Suspend the main thread until we are done
//
gWinNt->SuspendThread (mNtMainThreadHandle);
mCpu->GetInterruptState (mCpu, &InterruptState);
}
//
// Get the current system tick
//
CurrentTick = gWinNt->GetTickCount ();
Delta = CurrentTick - mNtLastTick;
mNtLastTick = CurrentTick;
//
// If delay was more then 1 second, ignore it (probably debugging case)
//
if (Delta < 1000) {
OriginalTPL = gBS->RaiseTPL (EFI_TPL_HIGH_LEVEL);
//
// Inform the firmware of an "timer interrupt". The time
// expired since the last call is 10,000 times the number
// of ms. (or 100ns units)
//
gWinNt->EnterCriticalSection (&mNtCriticalSection);
CallbackFunction = mTimerNotifyFunction;
gWinNt->LeaveCriticalSection (&mNtCriticalSection);
//
// Only invoke the callback function if a Non-NULL handler has been
// registered. Assume all other handlers are legal.
//
if (CallbackFunction != NULL) {
CallbackFunction ((UINT64) (Delta * 10000));
}
gBS->RestoreTPL (OriginalTPL);
}
//
// Resume the main thread
//
gWinNt->ResumeThread (mNtMainThreadHandle);
} else {
gWinNt->timeKillEvent (wTimerID);
mMMTimerThreadID = 0;
}
}
UINT
CreateNtTimer (
VOID
)
/*++
Routine Description:
It is used to emulate a platform
timer-driver interrupt handler.
Returns:
Timer ID
--*/
// TODO: function comment is missing 'Arguments:'
{
UINT32 SleepCount;
//
// Set our thread priority higher than the "main" thread.
//
gWinNt->SetThreadPriority (
gWinNt->GetCurrentThread (),
THREAD_PRIORITY_HIGHEST
);
//
// Calc the appropriate interval
//
gWinNt->EnterCriticalSection (&mNtCriticalSection);
SleepCount = (UINT32) (mTimerPeriod + 5000) / 10000;
gWinNt->LeaveCriticalSection (&mNtCriticalSection);
return gWinNt->timeSetEvent (
SleepCount,
0,
MMTimerThread,
(DWORD_PTR) NULL,
TIME_PERIODIC | TIME_KILL_SYNCHRONOUS | TIME_CALLBACK_FUNCTION
);
}
EFI_STATUS
EFIAPI
WinNtTimerDriverRegisterHandler (
IN EFI_TIMER_ARCH_PROTOCOL *This,
IN EFI_TIMER_NOTIFY NotifyFunction
)
/*++
Routine Description:
This function registers the handler NotifyFunction so it is called every time
the timer interrupt fires. It also passes the amount of time since the last
handler call to the NotifyFunction. If NotifyFunction is NULL, then the
handler is unregistered. If the handler is registered, then EFI_SUCCESS is
returned. If the CPU does not support registering a timer interrupt handler,
then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
when a handler is already registered, then EFI_ALREADY_STARTED is returned.
If an attempt is made to unregister a handler when a handler is not registered,
then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
is returned.
Arguments:
This - The EFI_TIMER_ARCH_PROTOCOL instance.
NotifyFunction - The function to call when a timer interrupt fires. This
function executes at TPL_HIGH_LEVEL. The DXE Core will
register a handler for the timer interrupt, so it can know
how much time has passed. This information is used to
signal timer based events. NULL will unregister the handler.
Returns:
EFI_SUCCESS - The timer handler was registered.
EFI_UNSUPPORTED - The platform does not support timer interrupts.
EFI_ALREADY_STARTED - NotifyFunction is not NULL, and a handler is already
registered.
EFI_INVALID_PARAMETER - NotifyFunction is NULL, and a handler was not
previously registered.
EFI_DEVICE_ERROR - The timer handler could not be registered.
--*/
{
//
// Check for invalid parameters
//
if (NotifyFunction == NULL && mTimerNotifyFunction == NULL) {
return EFI_INVALID_PARAMETER;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?