2008-05-21 03:40:12 +02:00
|
|
|
/** @file
|
2008-04-09 09:07:50 +02:00
|
|
|
Core Timer Services
|
|
|
|
|
2010-04-24 11:49:11 +02:00
|
|
|
Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
|
|
|
|
This program and the accompanying materials
|
2007-07-04 12:51:54 +02:00
|
|
|
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.
|
|
|
|
|
2008-04-09 09:07:50 +02:00
|
|
|
**/
|
2007-07-04 12:51:54 +02:00
|
|
|
|
|
|
|
|
2008-08-27 16:29:23 +02:00
|
|
|
#include "DxeMain.h"
|
2008-09-23 09:35:34 +02:00
|
|
|
#include "Event.h"
|
2007-07-04 12:51:54 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Internal data
|
|
|
|
//
|
|
|
|
|
2008-07-18 11:50:09 +02:00
|
|
|
LIST_ENTRY mEfiTimerList = INITIALIZE_LIST_HEAD_VARIABLE (mEfiTimerList);
|
|
|
|
EFI_LOCK mEfiTimerLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL - 1);
|
|
|
|
EFI_EVENT mEfiCheckTimerEvent = NULL;
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2008-07-18 11:50:09 +02:00
|
|
|
EFI_LOCK mEfiSystemTimeLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL);
|
|
|
|
UINT64 mEfiSystemTime = 0;
|
2007-07-04 12:51:54 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Timer functions
|
|
|
|
//
|
2008-05-09 09:08:30 +02:00
|
|
|
/**
|
2008-09-17 15:32:52 +02:00
|
|
|
Inserts the timer event.
|
|
|
|
|
|
|
|
@param Event Points to the internal structure of timer event
|
|
|
|
to be installed
|
2008-05-09 09:08:30 +02:00
|
|
|
|
|
|
|
**/
|
2007-07-04 12:51:54 +02:00
|
|
|
VOID
|
2008-09-17 15:32:52 +02:00
|
|
|
CoreInsertEventTimer (
|
|
|
|
IN IEVENT *Event
|
2007-07-04 12:51:54 +02:00
|
|
|
)
|
|
|
|
{
|
2008-09-17 15:32:52 +02:00
|
|
|
UINT64 TriggerTime;
|
|
|
|
LIST_ENTRY *Link;
|
|
|
|
IEVENT *Event2;
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2008-09-17 15:32:52 +02:00
|
|
|
ASSERT_LOCKED (&mEfiTimerLock);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Get the timer's trigger time
|
|
|
|
//
|
2010-01-19 07:42:21 +01:00
|
|
|
TriggerTime = Event->Timer.TriggerTime;
|
2008-09-17 15:32:52 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Insert the timer into the timer database in assending sorted order
|
|
|
|
//
|
|
|
|
for (Link = mEfiTimerList.ForwardLink; Link != &mEfiTimerList; Link = Link->ForwardLink) {
|
2010-01-19 07:42:21 +01:00
|
|
|
Event2 = CR (Link, IEVENT, Timer.Link, EVENT_SIGNATURE);
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2010-01-19 07:42:21 +01:00
|
|
|
if (Event2->Timer.TriggerTime > TriggerTime) {
|
2008-09-17 15:32:52 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-19 07:42:21 +01:00
|
|
|
InsertTailList (Link, &Event->Timer.Link);
|
2008-09-17 15:32:52 +02:00
|
|
|
}
|
2008-05-09 09:08:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the current system time.
|
|
|
|
|
|
|
|
@return The current system time
|
|
|
|
|
|
|
|
**/
|
2007-07-04 12:51:54 +02:00
|
|
|
UINT64
|
|
|
|
CoreCurrentSystemTime (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINT64 SystemTime;
|
|
|
|
|
|
|
|
CoreAcquireLock (&mEfiSystemTimeLock);
|
|
|
|
SystemTime = mEfiSystemTime;
|
|
|
|
CoreReleaseLock (&mEfiSystemTimeLock);
|
2008-07-24 04:54:45 +02:00
|
|
|
|
2007-07-04 12:51:54 +02:00
|
|
|
return SystemTime;
|
|
|
|
}
|
|
|
|
|
2008-05-09 09:08:30 +02:00
|
|
|
/**
|
|
|
|
Checks the sorted timer list against the current system time.
|
|
|
|
Signals any expired event timer.
|
|
|
|
|
2008-07-24 04:54:45 +02:00
|
|
|
@param CheckEvent Not used
|
2008-05-09 09:08:30 +02:00
|
|
|
@param Context Not used
|
|
|
|
|
|
|
|
**/
|
2007-07-04 12:51:54 +02:00
|
|
|
VOID
|
|
|
|
EFIAPI
|
|
|
|
CoreCheckTimers (
|
|
|
|
IN EFI_EVENT CheckEvent,
|
|
|
|
IN VOID *Context
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINT64 SystemTime;
|
|
|
|
IEVENT *Event;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Check the timer database for expired timers
|
|
|
|
//
|
|
|
|
CoreAcquireLock (&mEfiTimerLock);
|
|
|
|
SystemTime = CoreCurrentSystemTime ();
|
|
|
|
|
|
|
|
while (!IsListEmpty (&mEfiTimerList)) {
|
2010-01-19 07:42:21 +01:00
|
|
|
Event = CR (mEfiTimerList.ForwardLink, IEVENT, Timer.Link, EVENT_SIGNATURE);
|
2007-07-04 12:51:54 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// If this timer is not expired, then we're done
|
|
|
|
//
|
2010-01-19 07:42:21 +01:00
|
|
|
if (Event->Timer.TriggerTime > SystemTime) {
|
2007-07-04 12:51:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Remove this timer from the timer queue
|
|
|
|
//
|
|
|
|
|
2010-01-19 07:42:21 +01:00
|
|
|
RemoveEntryList (&Event->Timer.Link);
|
|
|
|
Event->Timer.Link.ForwardLink = NULL;
|
2007-07-04 12:51:54 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Signal it
|
|
|
|
//
|
|
|
|
CoreSignalEvent (Event);
|
|
|
|
|
|
|
|
//
|
|
|
|
// If this is a periodic timer, set it
|
|
|
|
//
|
2010-01-19 07:42:21 +01:00
|
|
|
if (Event->Timer.Period != 0) {
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
|
|
|
// Compute the timers new trigger time
|
|
|
|
//
|
2010-01-19 07:42:21 +01:00
|
|
|
Event->Timer.TriggerTime = Event->Timer.TriggerTime + Event->Timer.Period;
|
2007-07-04 12:51:54 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// If that's before now, then reset the timer to start from now
|
|
|
|
//
|
2010-01-19 07:42:21 +01:00
|
|
|
if (Event->Timer.TriggerTime <= SystemTime) {
|
|
|
|
Event->Timer.TriggerTime = SystemTime;
|
2007-07-04 12:51:54 +02:00
|
|
|
CoreSignalEvent (mEfiCheckTimerEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Add the timer
|
|
|
|
//
|
|
|
|
CoreInsertEventTimer (Event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CoreReleaseLock (&mEfiTimerLock);
|
|
|
|
}
|
|
|
|
|
2008-05-09 09:08:30 +02:00
|
|
|
|
|
|
|
/**
|
2008-09-17 15:32:52 +02:00
|
|
|
Initializes timer support.
|
2008-05-09 09:08:30 +02:00
|
|
|
|
2008-09-17 15:32:52 +02:00
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
CoreInitializeTimer (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
|
|
|
|
Status = CoreCreateEvent (
|
|
|
|
EVT_NOTIFY_SIGNAL,
|
|
|
|
TPL_HIGH_LEVEL - 1,
|
|
|
|
CoreCheckTimers,
|
|
|
|
NULL,
|
|
|
|
&mEfiCheckTimerEvent
|
|
|
|
);
|
|
|
|
ASSERT_EFI_ERROR (Status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Called by the platform code to process a tick.
|
|
|
|
|
|
|
|
@param Duration The number of 100ns elasped since the last call
|
|
|
|
to TimerTick
|
2008-05-09 09:08:30 +02:00
|
|
|
|
|
|
|
**/
|
2007-07-04 12:51:54 +02:00
|
|
|
VOID
|
2008-09-17 15:32:52 +02:00
|
|
|
EFIAPI
|
|
|
|
CoreTimerTick (
|
|
|
|
IN UINT64 Duration
|
2007-07-04 12:51:54 +02:00
|
|
|
)
|
|
|
|
{
|
2008-09-17 15:32:52 +02:00
|
|
|
IEVENT *Event;
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2008-09-17 15:32:52 +02:00
|
|
|
//
|
|
|
|
// Check runtiem flag in case there are ticks while exiting boot services
|
|
|
|
//
|
|
|
|
CoreAcquireLock (&mEfiSystemTimeLock);
|
2007-07-04 12:51:54 +02:00
|
|
|
|
|
|
|
//
|
2008-09-17 15:32:52 +02:00
|
|
|
// Update the system time
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
2008-09-17 15:32:52 +02:00
|
|
|
mEfiSystemTime += Duration;
|
2007-07-04 12:51:54 +02:00
|
|
|
|
|
|
|
//
|
2008-09-17 15:32:52 +02:00
|
|
|
// If the head of the list is expired, fire the timer event
|
|
|
|
// to process it
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
2008-09-17 15:32:52 +02:00
|
|
|
if (!IsListEmpty (&mEfiTimerList)) {
|
2010-01-19 07:42:21 +01:00
|
|
|
Event = CR (mEfiTimerList.ForwardLink, IEVENT, Timer.Link, EVENT_SIGNATURE);
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2010-01-19 07:42:21 +01:00
|
|
|
if (Event->Timer.TriggerTime <= mEfiSystemTime) {
|
2008-09-17 15:32:52 +02:00
|
|
|
CoreSignalEvent (mEfiCheckTimerEvent);
|
2007-07-04 12:51:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-17 15:32:52 +02:00
|
|
|
CoreReleaseLock (&mEfiSystemTimeLock);
|
2007-07-04 12:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-05-09 09:08:30 +02:00
|
|
|
/**
|
|
|
|
Sets the type of timer and the trigger time for a timer event.
|
|
|
|
|
2008-07-24 04:54:45 +02:00
|
|
|
@param UserEvent The timer event that is to be signaled at the
|
|
|
|
specified time
|
|
|
|
@param Type The type of time that is specified in
|
|
|
|
TriggerTime
|
|
|
|
@param TriggerTime The number of 100ns units until the timer
|
|
|
|
expires
|
2008-05-09 09:08:30 +02:00
|
|
|
|
2008-07-24 04:54:45 +02:00
|
|
|
@retval EFI_SUCCESS The event has been set to be signaled at the
|
|
|
|
requested time
|
2008-05-09 09:08:30 +02:00
|
|
|
@retval EFI_INVALID_PARAMETER Event or Type is not valid
|
|
|
|
|
|
|
|
**/
|
2007-07-04 12:51:54 +02:00
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
CoreSetTimer (
|
|
|
|
IN EFI_EVENT UserEvent,
|
|
|
|
IN EFI_TIMER_DELAY Type,
|
|
|
|
IN UINT64 TriggerTime
|
|
|
|
)
|
|
|
|
{
|
|
|
|
IEVENT *Event;
|
|
|
|
|
|
|
|
Event = UserEvent;
|
|
|
|
|
|
|
|
if (Event == NULL) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Event->Signature != EVENT_SIGNATURE) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2008-10-21 05:11:47 +02:00
|
|
|
if (Type < 0 || Type > TimerRelative || (Event->Type & EVT_TIMER) == 0) {
|
2007-07-04 12:51:54 +02:00
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
CoreAcquireLock (&mEfiTimerLock);
|
|
|
|
|
|
|
|
//
|
|
|
|
// If the timer is queued to the timer database, remove it
|
|
|
|
//
|
2010-01-19 07:42:21 +01:00
|
|
|
if (Event->Timer.Link.ForwardLink != NULL) {
|
|
|
|
RemoveEntryList (&Event->Timer.Link);
|
|
|
|
Event->Timer.Link.ForwardLink = NULL;
|
2007-07-04 12:51:54 +02:00
|
|
|
}
|
|
|
|
|
2010-01-19 07:42:21 +01:00
|
|
|
Event->Timer.TriggerTime = 0;
|
|
|
|
Event->Timer.Period = 0;
|
2007-07-04 12:51:54 +02:00
|
|
|
|
|
|
|
if (Type != TimerCancel) {
|
|
|
|
|
|
|
|
if (Type == TimerPeriodic) {
|
2010-01-19 07:42:21 +01:00
|
|
|
Event->Timer.Period = TriggerTime;
|
2007-07-04 12:51:54 +02:00
|
|
|
}
|
|
|
|
|
2010-01-19 07:42:21 +01:00
|
|
|
Event->Timer.TriggerTime = CoreCurrentSystemTime () + TriggerTime;
|
2007-07-04 12:51:54 +02:00
|
|
|
CoreInsertEventTimer (Event);
|
|
|
|
|
|
|
|
if (TriggerTime == 0) {
|
|
|
|
CoreSignalEvent (mEfiCheckTimerEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CoreReleaseLock (&mEfiTimerLock);
|
2008-07-18 11:50:09 +02:00
|
|
|
|
2007-07-04 12:51:54 +02:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|