4 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 LIST_ENTRY mEfiTimerList
= INITIALIZE_LIST_HEAD_VARIABLE (mEfiTimerList
);
23 EFI_LOCK mEfiTimerLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL
- 1);
24 EFI_EVENT mEfiCheckTimerEvent
= NULL
;
26 EFI_LOCK mEfiSystemTimeLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL
);
27 UINT64 mEfiSystemTime
= 0;
33 Inserts the timer event.
35 @param Event Points to the internal structure of timer event
40 CoreInsertEventTimer (
48 ASSERT_LOCKED (&mEfiTimerLock
);
51 // Get the timer's trigger time
53 TriggerTime
= Event
->u
.Timer
.TriggerTime
;
56 // Insert the timer into the timer database in assending sorted order
58 for (Link
= mEfiTimerList
.ForwardLink
; Link
!= &mEfiTimerList
; Link
= Link
->ForwardLink
) {
59 Event2
= CR (Link
, IEVENT
, u
.Timer
.Link
, EVENT_SIGNATURE
);
61 if (Event2
->u
.Timer
.TriggerTime
> TriggerTime
) {
66 InsertTailList (Link
, &Event
->u
.Timer
.Link
);
70 Returns the current system time.
72 @return The current system time
76 CoreCurrentSystemTime (
82 CoreAcquireLock (&mEfiSystemTimeLock
);
83 SystemTime
= mEfiSystemTime
;
84 CoreReleaseLock (&mEfiSystemTimeLock
);
90 Checks the sorted timer list against the current system time.
91 Signals any expired event timer.
93 @param CheckEvent Not used
94 @param Context Not used
100 IN EFI_EVENT CheckEvent
,
108 // Check the timer database for expired timers
110 CoreAcquireLock (&mEfiTimerLock
);
111 SystemTime
= CoreCurrentSystemTime ();
113 while (!IsListEmpty (&mEfiTimerList
)) {
114 Event
= CR (mEfiTimerList
.ForwardLink
, IEVENT
, u
.Timer
.Link
, EVENT_SIGNATURE
);
117 // If this timer is not expired, then we're done
119 if (Event
->u
.Timer
.TriggerTime
> SystemTime
) {
124 // Remove this timer from the timer queue
127 RemoveEntryList (&Event
->u
.Timer
.Link
);
128 Event
->u
.Timer
.Link
.ForwardLink
= NULL
;
133 CoreSignalEvent (Event
);
136 // If this is a periodic timer, set it
138 if (Event
->u
.Timer
.Period
) {
140 // Compute the timers new trigger time
142 Event
->u
.Timer
.TriggerTime
= Event
->u
.Timer
.TriggerTime
+ Event
->u
.Timer
.Period
;
145 // If that's before now, then reset the timer to start from now
147 if (Event
->u
.Timer
.TriggerTime
<= SystemTime
) {
148 Event
->u
.Timer
.TriggerTime
= SystemTime
;
149 CoreSignalEvent (mEfiCheckTimerEvent
);
155 CoreInsertEventTimer (Event
);
159 CoreReleaseLock (&mEfiTimerLock
);
164 Initializes timer support.
168 CoreInitializeTimer (
174 Status
= CoreCreateEvent (
181 ASSERT_EFI_ERROR (Status
);
186 Called by the platform code to process a tick.
188 @param Duration The number of 100ns elasped since the last call
201 // Check runtiem flag in case there are ticks while exiting boot services
203 CoreAcquireLock (&mEfiSystemTimeLock
);
206 // Update the system time
208 mEfiSystemTime
+= Duration
;
211 // If the head of the list is expired, fire the timer event
214 if (!IsListEmpty (&mEfiTimerList
)) {
215 Event
= CR (mEfiTimerList
.ForwardLink
, IEVENT
, u
.Timer
.Link
, EVENT_SIGNATURE
);
217 if (Event
->u
.Timer
.TriggerTime
<= mEfiSystemTime
) {
218 CoreSignalEvent (mEfiCheckTimerEvent
);
222 CoreReleaseLock (&mEfiSystemTimeLock
);
228 Sets the type of timer and the trigger time for a timer event.
230 @param UserEvent The timer event that is to be signaled at the
232 @param Type The type of time that is specified in
234 @param TriggerTime The number of 100ns units until the timer
237 @retval EFI_SUCCESS The event has been set to be signaled at the
239 @retval EFI_INVALID_PARAMETER Event or Type is not valid
245 IN EFI_EVENT UserEvent
,
246 IN EFI_TIMER_DELAY Type
,
247 IN UINT64 TriggerTime
255 return EFI_INVALID_PARAMETER
;
258 if (Event
->Signature
!= EVENT_SIGNATURE
) {
259 return EFI_INVALID_PARAMETER
;
262 if (Type
< 0 || Type
> TimerRelative
|| !(Event
->Type
& EVT_TIMER
)) {
263 return EFI_INVALID_PARAMETER
;
266 CoreAcquireLock (&mEfiTimerLock
);
269 // If the timer is queued to the timer database, remove it
271 if (Event
->u
.Timer
.Link
.ForwardLink
!= NULL
) {
272 RemoveEntryList (&Event
->u
.Timer
.Link
);
273 Event
->u
.Timer
.Link
.ForwardLink
= NULL
;
276 Event
->u
.Timer
.TriggerTime
= 0;
277 Event
->u
.Timer
.Period
= 0;
279 if (Type
!= TimerCancel
) {
281 if (Type
== TimerPeriodic
) {
282 Event
->u
.Timer
.Period
= TriggerTime
;
285 Event
->u
.Timer
.TriggerTime
= CoreCurrentSystemTime () + TriggerTime
;
286 CoreInsertEventTimer (Event
);
288 if (TriggerTime
== 0) {
289 CoreSignalEvent (mEfiCheckTimerEvent
);
293 CoreReleaseLock (&mEfiTimerLock
);