+++ /dev/null
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution. The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-Module Name:\r
-\r
- timer.c\r
-\r
-Abstract:\r
-\r
- EFI Event support\r
-\r
-Revision History\r
-\r
---*/\r
-\r
-\r
-#include <DxeMain.h>\r
-\r
-//\r
-// Internal prototypes\r
-//\r
-STATIC\r
-UINT64\r
-CoreCurrentSystemTime (\r
- VOID\r
- );\r
-\r
-STATIC\r
-VOID\r
-EFIAPI\r
-CoreCheckTimers (\r
- IN EFI_EVENT Event,\r
- IN VOID *Context\r
- );\r
-\r
-STATIC\r
-VOID\r
-CoreInsertEventTimer (\r
- IN IEVENT *Event\r
- );\r
-\r
-//\r
-// Internal data\r
-//\r
-\r
-static LIST_ENTRY mEfiTimerList = INITIALIZE_LIST_HEAD_VARIABLE (mEfiTimerList);\r
-static EFI_LOCK mEfiTimerLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL - 1);\r
-static EFI_EVENT mEfiCheckTimerEvent;\r
-\r
-static EFI_LOCK mEfiSystemTimeLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL);\r
-static UINT64 mEfiSystemTime = 0;\r
-\r
-//\r
-// Timer functions\r
-//\r
-\r
-VOID\r
-CoreInitializeTimer (\r
- VOID\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Initializes timer support\r
-\r
-Arguments:\r
-\r
- None\r
-\r
-Returns:\r
-\r
- None\r
-\r
---*/\r
-{\r
- EFI_STATUS Status;\r
-\r
- Status = CoreCreateEvent (\r
- EVT_NOTIFY_SIGNAL,\r
- TPL_HIGH_LEVEL - 1,\r
- CoreCheckTimers,\r
- NULL,\r
- &mEfiCheckTimerEvent\r
- );\r
- ASSERT_EFI_ERROR (Status);\r
-}\r
-\r
-STATIC\r
-UINT64\r
-CoreCurrentSystemTime (\r
- VOID\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Returns the current system time\r
-\r
-Arguments:\r
-\r
- None\r
-\r
-Returns:\r
-\r
- Returns the current system time\r
-\r
---*/\r
-{\r
- UINT64 SystemTime;\r
-\r
- CoreAcquireLock (&mEfiSystemTimeLock);\r
- SystemTime = mEfiSystemTime;\r
- CoreReleaseLock (&mEfiSystemTimeLock);\r
- return SystemTime;\r
-}\r
-\r
-VOID\r
-EFIAPI\r
-CoreTimerTick (\r
- IN UINT64 Duration\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Called by the platform code to process a tick.\r
-\r
-Arguments:\r
-\r
- Duration - The number of 100ns elasped since the last call to TimerTick\r
-\r
-Returns:\r
-\r
- None\r
-\r
---*/\r
-{\r
- IEVENT *Event;\r
-\r
- //\r
- // Check runtiem flag in case there are ticks while exiting boot services\r
- //\r
-\r
- CoreAcquireLock (&mEfiSystemTimeLock);\r
-\r
- //\r
- // Update the system time\r
- //\r
-\r
- mEfiSystemTime += Duration;\r
-\r
- //\r
- // If the head of the list is expired, fire the timer event\r
- // to process it\r
- //\r
-\r
- if (!IsListEmpty (&mEfiTimerList)) {\r
- Event = CR (mEfiTimerList.ForwardLink, IEVENT, u.Timer.Link, EVENT_SIGNATURE);\r
-\r
- if (Event->u.Timer.TriggerTime <= mEfiSystemTime) {\r
- CoreSignalEvent (mEfiCheckTimerEvent);\r
- }\r
- }\r
-\r
- CoreReleaseLock (&mEfiSystemTimeLock);\r
-}\r
-\r
-STATIC\r
-VOID\r
-EFIAPI\r
-CoreCheckTimers (\r
- IN EFI_EVENT CheckEvent,\r
- IN VOID *Context\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Checks the sorted timer list against the current system time.\r
- Signals any expired event timer.\r
-\r
-Arguments:\r
-\r
- CheckEvent - Not used\r
-\r
- Context - Not used\r
-\r
-Returns:\r
-\r
- None\r
-\r
---*/\r
-{\r
- UINT64 SystemTime;\r
- IEVENT *Event;\r
-\r
- //\r
- // Check the timer database for expired timers\r
- //\r
-\r
- CoreAcquireLock (&mEfiTimerLock);\r
- SystemTime = CoreCurrentSystemTime ();\r
-\r
- while (!IsListEmpty (&mEfiTimerList)) {\r
- Event = CR (mEfiTimerList.ForwardLink, IEVENT, u.Timer.Link, EVENT_SIGNATURE);\r
-\r
- //\r
- // If this timer is not expired, then we're done\r
- //\r
-\r
- if (Event->u.Timer.TriggerTime > SystemTime) {\r
- break;\r
- }\r
-\r
- //\r
- // Remove this timer from the timer queue\r
- //\r
-\r
- RemoveEntryList (&Event->u.Timer.Link);\r
- Event->u.Timer.Link.ForwardLink = NULL;\r
-\r
- //\r
- // Signal it\r
- //\r
- CoreSignalEvent (Event);\r
-\r
- //\r
- // If this is a periodic timer, set it\r
- //\r
- if (Event->u.Timer.Period) {\r
-\r
- //\r
- // Compute the timers new trigger time\r
- //\r
-\r
- Event->u.Timer.TriggerTime = Event->u.Timer.TriggerTime + Event->u.Timer.Period;\r
-\r
- //\r
- // If that's before now, then reset the timer to start from now\r
- //\r
- if (Event->u.Timer.TriggerTime <= SystemTime) {\r
- Event->u.Timer.TriggerTime = SystemTime;\r
- CoreSignalEvent (mEfiCheckTimerEvent);\r
- }\r
-\r
- //\r
- // Add the timer\r
- //\r
-\r
- CoreInsertEventTimer (Event);\r
- }\r
- }\r
-\r
- CoreReleaseLock (&mEfiTimerLock);\r
-}\r
-\r
-STATIC\r
-VOID\r
-CoreInsertEventTimer (\r
- IN IEVENT *Event\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Inserts the timer event\r
-\r
-Arguments:\r
-\r
- Event - Points to the internal structure of timer event to be installed\r
-\r
-Returns:\r
-\r
- None\r
-\r
---*/\r
-{\r
- UINT64 TriggerTime;\r
- LIST_ENTRY *Link;\r
- IEVENT *Event2;\r
-\r
- ASSERT_LOCKED (&mEfiTimerLock);\r
-\r
- //\r
- // Get the timer's trigger time\r
- //\r
-\r
- TriggerTime = Event->u.Timer.TriggerTime;\r
-\r
- //\r
- // Insert the timer into the timer database in assending sorted order\r
- //\r
-\r
- for (Link = mEfiTimerList.ForwardLink; Link != &mEfiTimerList; Link = Link->ForwardLink) {\r
- Event2 = CR (Link, IEVENT, u.Timer.Link, EVENT_SIGNATURE);\r
-\r
- if (Event2->u.Timer.TriggerTime > TriggerTime) {\r
- break;\r
- }\r
- }\r
-\r
- InsertTailList (Link, &Event->u.Timer.Link);\r
-}\r
-\r
-\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-CoreSetTimer (\r
- IN EFI_EVENT UserEvent,\r
- IN EFI_TIMER_DELAY Type,\r
- IN UINT64 TriggerTime\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- Sets the type of timer and the trigger time for a timer event.\r
-\r
-Arguments:\r
-\r
- UserEvent - The timer event that is to be signaled at the specified time\r
- Type - The type of time that is specified in TriggerTime\r
- TriggerTime - The number of 100ns units until the timer expires\r
-\r
-Returns:\r
-\r
- EFI_SUCCESS - The event has been set to be signaled at the requested time\r
- EFI_INVALID_PARAMETER - Event or Type is not valid\r
-\r
---*/\r
-{\r
- IEVENT *Event;\r
-\r
- Event = UserEvent;\r
-\r
- if (Event == NULL) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
-\r
- if (Event->Signature != EVENT_SIGNATURE) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
-\r
- if (Type < 0 || Type > TimerRelative || !(Event->Type & EVT_TIMER)) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
-\r
- CoreAcquireLock (&mEfiTimerLock);\r
-\r
- //\r
- // If the timer is queued to the timer database, remove it\r
- //\r
-\r
- if (Event->u.Timer.Link.ForwardLink != NULL) {\r
- RemoveEntryList (&Event->u.Timer.Link);\r
- Event->u.Timer.Link.ForwardLink = NULL;\r
- }\r
-\r
- Event->u.Timer.TriggerTime = 0;\r
- Event->u.Timer.Period = 0;\r
-\r
- if (Type != TimerCancel) {\r
-\r
- if (Type == TimerPeriodic) {\r
- Event->u.Timer.Period = TriggerTime;\r
- }\r
-\r
- Event->u.Timer.TriggerTime = CoreCurrentSystemTime () + TriggerTime;\r
- CoreInsertEventTimer (Event);\r
-\r
- if (TriggerTime == 0) {\r
- CoreSignalEvent (mEfiCheckTimerEvent);\r
- }\r
- }\r
-\r
- CoreReleaseLock (&mEfiTimerLock);\r
- return EFI_SUCCESS;\r
-}\r