3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
28 // Internal prototypes
32 CoreCurrentSystemTime (
46 CoreInsertEventTimer (
54 static LIST_ENTRY mEfiTimerList
= INITIALIZE_LIST_HEAD_VARIABLE (mEfiTimerList
);
55 static EFI_LOCK mEfiTimerLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL
- 1);
56 static EFI_EVENT mEfiCheckTimerEvent
;
58 static EFI_LOCK mEfiSystemTimeLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL
);
59 static UINT64 mEfiSystemTime
= 0;
73 Initializes timer support
87 Status
= CoreCreateEvent (
94 ASSERT_EFI_ERROR (Status
);
99 CoreCurrentSystemTime (
106 Returns the current system time
114 Returns the current system time
120 CoreAcquireLock (&mEfiSystemTimeLock
);
121 SystemTime
= mEfiSystemTime
;
122 CoreReleaseLock (&mEfiSystemTimeLock
);
135 Called by the platform code to process a tick.
139 Duration - The number of 100ns elasped since the last call to TimerTick
150 // Check runtiem flag in case there are ticks while exiting boot services
153 CoreAcquireLock (&mEfiSystemTimeLock
);
156 // Update the system time
159 mEfiSystemTime
+= Duration
;
162 // If the head of the list is expired, fire the timer event
166 if (!IsListEmpty (&mEfiTimerList
)) {
167 Event
= CR (mEfiTimerList
.ForwardLink
, IEVENT
, u
.Timer
.Link
, EVENT_SIGNATURE
);
169 if (Event
->u
.Timer
.TriggerTime
<= mEfiSystemTime
) {
170 CoreSignalEvent (mEfiCheckTimerEvent
);
174 CoreReleaseLock (&mEfiSystemTimeLock
);
181 IN EFI_EVENT CheckEvent
,
188 Checks the sorted timer list against the current system time.
189 Signals any expired event timer.
193 CheckEvent - Not used
207 // Check the timer database for expired timers
210 CoreAcquireLock (&mEfiTimerLock
);
211 SystemTime
= CoreCurrentSystemTime ();
213 while (!IsListEmpty (&mEfiTimerList
)) {
214 Event
= CR (mEfiTimerList
.ForwardLink
, IEVENT
, u
.Timer
.Link
, EVENT_SIGNATURE
);
217 // If this timer is not expired, then we're done
220 if (Event
->u
.Timer
.TriggerTime
> SystemTime
) {
225 // Remove this timer from the timer queue
228 RemoveEntryList (&Event
->u
.Timer
.Link
);
229 Event
->u
.Timer
.Link
.ForwardLink
= NULL
;
234 CoreSignalEvent (Event
);
237 // If this is a periodic timer, set it
239 if (Event
->u
.Timer
.Period
) {
242 // Compute the timers new trigger time
245 Event
->u
.Timer
.TriggerTime
= Event
->u
.Timer
.TriggerTime
+ Event
->u
.Timer
.Period
;
248 // If that's before now, then reset the timer to start from now
250 if (Event
->u
.Timer
.TriggerTime
<= SystemTime
) {
251 Event
->u
.Timer
.TriggerTime
= SystemTime
;
252 CoreSignalEvent (mEfiCheckTimerEvent
);
259 CoreInsertEventTimer (Event
);
263 CoreReleaseLock (&mEfiTimerLock
);
268 CoreInsertEventTimer (
275 Inserts the timer event
279 Event - Points to the internal structure of timer event to be installed
291 ASSERT_LOCKED (&mEfiTimerLock
);
294 // Get the timer's trigger time
297 TriggerTime
= Event
->u
.Timer
.TriggerTime
;
300 // Insert the timer into the timer database in assending sorted order
303 for (Link
= mEfiTimerList
.ForwardLink
; Link
!= &mEfiTimerList
; Link
= Link
->ForwardLink
) {
304 Event2
= CR (Link
, IEVENT
, u
.Timer
.Link
, EVENT_SIGNATURE
);
306 if (Event2
->u
.Timer
.TriggerTime
> TriggerTime
) {
311 InsertTailList (Link
, &Event
->u
.Timer
.Link
);
319 IN EFI_EVENT UserEvent
,
320 IN EFI_TIMER_DELAY Type
,
321 IN UINT64 TriggerTime
327 Sets the type of timer and the trigger time for a timer event.
331 UserEvent - The timer event that is to be signaled at the specified time
332 Type - The type of time that is specified in TriggerTime
333 TriggerTime - The number of 100ns units until the timer expires
337 EFI_SUCCESS - The event has been set to be signaled at the requested time
338 EFI_INVALID_PARAMETER - Event or Type is not valid
347 return EFI_INVALID_PARAMETER
;
350 if (Event
->Signature
!= EVENT_SIGNATURE
) {
351 return EFI_INVALID_PARAMETER
;
354 if (Type
< 0 || Type
> TimerRelative
|| !(Event
->Type
& EVT_TIMER
)) {
355 return EFI_INVALID_PARAMETER
;
358 CoreAcquireLock (&mEfiTimerLock
);
361 // If the timer is queued to the timer database, remove it
364 if (Event
->u
.Timer
.Link
.ForwardLink
!= NULL
) {
365 RemoveEntryList (&Event
->u
.Timer
.Link
);
366 Event
->u
.Timer
.Link
.ForwardLink
= NULL
;
369 Event
->u
.Timer
.TriggerTime
= 0;
370 Event
->u
.Timer
.Period
= 0;
372 if (Type
!= TimerCancel
) {
374 if (Type
== TimerPeriodic
) {
375 Event
->u
.Timer
.Period
= TriggerTime
;
378 Event
->u
.Timer
.TriggerTime
= CoreCurrentSystemTime () + TriggerTime
;
379 CoreInsertEventTimer (Event
);
381 if (TriggerTime
== 0) {
382 CoreSignalEvent (mEfiCheckTimerEvent
);
386 CoreReleaseLock (&mEfiTimerLock
);