2 UEFI Event support functions implemented in this file.
4 Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
5 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.
20 /// gEfiCurrentTpl - Current Task priority level
22 EFI_TPL gEfiCurrentTpl
= TPL_APPLICATION
;
25 /// gEventQueueLock - Protects the event queus
27 EFI_LOCK gEventQueueLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL
);
30 /// gEventQueue - A list of event's to notify for each priority level
32 LIST_ENTRY gEventQueue
[TPL_HIGH_LEVEL
+ 1];
35 /// gEventPending - A bitmask of the EventQueues that are pending
37 UINTN gEventPending
= 0;
40 /// gEventSignalQueue - A list of events to signal based on EventGroup type
42 LIST_ENTRY gEventSignalQueue
= INITIALIZE_LIST_HEAD_VARIABLE (gEventSignalQueue
);
45 /// Enumerate the valid types
47 UINT32 mEventTable
[] = {
49 /// 0x80000200 Timer event with a notification function that is
50 /// queue when the event is signaled with SignalEvent()
52 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
54 /// 0x80000000 Timer event without a notification function. It can be
55 /// signaled with SignalEvent() and checked with CheckEvent() or WaitForEvent().
59 /// 0x00000100 Generic event with a notification function that
60 /// can be waited on with CheckEvent() or WaitForEvent()
64 /// 0x00000200 Generic event with a notification function that
65 /// is queue when the event is signaled with SignalEvent()
69 /// 0x00000201 ExitBootServicesEvent.
71 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
73 /// 0x60000202 SetVirtualAddressMapEvent.
75 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
,
78 /// 0x00000000 Generic event without a notification function.
79 /// It can be signaled with SignalEvent() and checked with CheckEvent()
80 /// or WaitForEvent().
84 /// 0x80000100 Timer event with a notification function that can be
85 /// waited on with CheckEvent() or WaitForEvent()
87 EVT_TIMER
| EVT_NOTIFY_WAIT
,
92 Enter critical section by acquiring the lock on gEventQueueLock.
96 CoreAcquireEventLock (
100 CoreAcquireLock (&gEventQueueLock
);
105 Exit critical section by releasing the lock on gEventQueueLock.
109 CoreReleaseEventLock (
113 CoreReleaseLock (&gEventQueueLock
);
119 Initializes "event" support.
121 @retval EFI_SUCCESS Always return success
125 CoreInitializeEventServices (
131 for (Index
=0; Index
<= TPL_HIGH_LEVEL
; Index
++) {
132 InitializeListHead (&gEventQueue
[Index
]);
135 CoreInitializeTimer ();
143 Dispatches all pending events.
145 @param Priority The task priority level of event notifications
150 CoreDispatchEventNotifies (
157 CoreAcquireEventLock ();
158 ASSERT (gEventQueueLock
.OwnerTpl
== Priority
);
159 Head
= &gEventQueue
[Priority
];
162 // Dispatch all the pending notifications
164 while (!IsListEmpty (Head
)) {
166 Event
= CR (Head
->ForwardLink
, IEVENT
, NotifyLink
, EVENT_SIGNATURE
);
167 RemoveEntryList (&Event
->NotifyLink
);
169 Event
->NotifyLink
.ForwardLink
= NULL
;
172 // Only clear the SIGNAL status if it is a SIGNAL type event.
173 // WAIT type events are only cleared in CheckEvent()
175 if ((Event
->Type
& EVT_NOTIFY_SIGNAL
) != 0) {
176 Event
->SignalCount
= 0;
179 CoreReleaseEventLock ();
184 ASSERT (Event
->NotifyFunction
!= NULL
);
185 Event
->NotifyFunction (Event
, Event
->NotifyContext
);
188 // Check for next pending event
190 CoreAcquireEventLock ();
193 gEventPending
&= ~(1 << Priority
);
194 CoreReleaseEventLock ();
200 Queues the event's notification function to fire.
202 @param Event The Event to notify
212 // Event database must be locked
214 ASSERT_LOCKED (&gEventQueueLock
);
217 // If the event is queued somewhere, remove it
220 if (Event
->NotifyLink
.ForwardLink
!= NULL
) {
221 RemoveEntryList (&Event
->NotifyLink
);
222 Event
->NotifyLink
.ForwardLink
= NULL
;
226 // Queue the event to the pending notification list
229 InsertTailList (&gEventQueue
[Event
->NotifyTpl
], &Event
->NotifyLink
);
230 gEventPending
|= (UINTN
)(1 << Event
->NotifyTpl
);
237 Signals all events in the EventGroup.
239 @param EventGroup The list to signal
243 CoreNotifySignalList (
244 IN EFI_GUID
*EventGroup
251 CoreAcquireEventLock ();
253 Head
= &gEventSignalQueue
;
254 for (Link
= Head
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
255 Event
= CR (Link
, IEVENT
, SignalLink
, EVENT_SIGNATURE
);
256 if (CompareGuid (&Event
->EventGroup
, EventGroup
)) {
257 CoreNotifyEvent (Event
);
261 CoreReleaseEventLock ();
266 Creates a general-purpose event structure.
268 @param Type The type of event to create and its mode and
270 @param NotifyTpl The task priority level of event notifications
271 @param NotifyFunction Pointer to the events notification function
272 @param NotifyContext Pointer to the notification functions context;
273 corresponds to parameter "Context" in the
274 notification function
275 @param Event Pointer to the newly created event if the call
276 succeeds; undefined otherwise
278 @retval EFI_SUCCESS The event structure was created
279 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value
280 @retval EFI_OUT_OF_RESOURCES The event could not be allocated
287 IN EFI_TPL NotifyTpl
,
288 IN EFI_EVENT_NOTIFY NotifyFunction
, OPTIONAL
289 IN VOID
*NotifyContext
, OPTIONAL
293 return CoreCreateEventEx (Type
, NotifyTpl
, NotifyFunction
, NotifyContext
, NULL
, Event
);
299 Creates a general-purpose event structure
301 @param Type The type of event to create and its mode and
303 @param NotifyTpl The task priority level of event notifications
304 @param NotifyFunction Pointer to the events notification function
305 @param NotifyContext Pointer to the notification functions context;
306 corresponds to parameter "Context" in the
307 notification function
308 @param EventGroup GUID for EventGroup if NULL act the same as
310 @param Event Pointer to the newly created event if the call
311 succeeds; undefined otherwise
313 @retval EFI_SUCCESS The event structure was created
314 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value
315 @retval EFI_OUT_OF_RESOURCES The event could not be allocated
322 IN EFI_TPL NotifyTpl
,
323 IN EFI_EVENT_NOTIFY NotifyFunction
, OPTIONAL
324 IN CONST VOID
*NotifyContext
, OPTIONAL
325 IN CONST EFI_GUID
*EventGroup
, OPTIONAL
335 return EFI_INVALID_PARAMETER
;
339 // Check to make sure no reserved flags are set
341 Status
= EFI_INVALID_PARAMETER
;
342 for (Index
= 0; Index
< (sizeof (mEventTable
) / sizeof (UINT32
)); Index
++) {
343 if (Type
== mEventTable
[Index
]) {
344 Status
= EFI_SUCCESS
;
348 if(EFI_ERROR (Status
)) {
349 return EFI_INVALID_PARAMETER
;
353 // Convert Event type for pre-defined Event groups
355 if (EventGroup
!= NULL
) {
357 // For event group, type EVT_SIGNAL_EXIT_BOOT_SERVICES and EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
360 if ((Type
== EVT_SIGNAL_EXIT_BOOT_SERVICES
) || (Type
== EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
)) {
361 return EFI_INVALID_PARAMETER
;
363 if (CompareGuid (EventGroup
, &gEfiEventExitBootServicesGuid
)) {
364 Type
= EVT_SIGNAL_EXIT_BOOT_SERVICES
;
365 } else if (CompareGuid (EventGroup
, &gEfiEventVirtualAddressChangeGuid
)) {
366 Type
= EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
;
370 // Convert EFI 1.10 Events to their UEFI 2.0 CreateEventEx mapping
372 if (Type
== EVT_SIGNAL_EXIT_BOOT_SERVICES
) {
373 EventGroup
= &gEfiEventExitBootServicesGuid
;
374 } else if (Type
== EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) {
375 EventGroup
= &gEfiEventVirtualAddressChangeGuid
;
380 // If it's a notify type of event, check its parameters
382 if ((Type
& (EVT_NOTIFY_WAIT
| EVT_NOTIFY_SIGNAL
)) != 0) {
384 // Check for an invalid NotifyFunction or NotifyTpl
386 if ((NotifyFunction
== NULL
) ||
387 (NotifyTpl
<= TPL_APPLICATION
) ||
388 (NotifyTpl
>= TPL_HIGH_LEVEL
)) {
389 return EFI_INVALID_PARAMETER
;
394 // No notification needed, zero ignored values
397 NotifyFunction
= NULL
;
398 NotifyContext
= NULL
;
402 // Allocate and initialize a new event structure.
404 if ((Type
& EVT_RUNTIME
) != 0) {
405 IEvent
= AllocateRuntimeZeroPool (sizeof (IEVENT
));
407 IEvent
= AllocateZeroPool (sizeof (IEVENT
));
409 if (IEvent
== NULL
) {
410 return EFI_OUT_OF_RESOURCES
;
413 IEvent
->Signature
= EVENT_SIGNATURE
;
416 IEvent
->NotifyTpl
= NotifyTpl
;
417 IEvent
->NotifyFunction
= NotifyFunction
;
418 IEvent
->NotifyContext
= (VOID
*)NotifyContext
;
419 if (EventGroup
!= NULL
) {
420 CopyGuid (&IEvent
->EventGroup
, EventGroup
);
421 IEvent
->ExFlag
= TRUE
;
426 if ((Type
& EVT_RUNTIME
) != 0) {
428 // Keep a list of all RT events so we can tell the RT AP.
430 IEvent
->RuntimeData
.Type
= Type
;
431 IEvent
->RuntimeData
.NotifyTpl
= NotifyTpl
;
432 IEvent
->RuntimeData
.NotifyFunction
= NotifyFunction
;
433 IEvent
->RuntimeData
.NotifyContext
= (VOID
*) NotifyContext
;
434 IEvent
->RuntimeData
.Event
= (EFI_EVENT
*) IEvent
;
435 InsertTailList (&gRuntime
->EventHead
, &IEvent
->RuntimeData
.Link
);
438 CoreAcquireEventLock ();
440 if ((Type
& EVT_NOTIFY_SIGNAL
) != 0x00000000) {
442 // The Event's NotifyFunction must be queued whenever the event is signaled
444 InsertHeadList (&gEventSignalQueue
, &IEvent
->SignalLink
);
447 CoreReleaseEventLock ();
459 Signals the event. Queues the event to be notified if needed.
461 @param UserEvent The event to signal .
463 @retval EFI_INVALID_PARAMETER Parameters are not valid.
464 @retval EFI_SUCCESS The event was signaled.
470 IN EFI_EVENT UserEvent
478 return EFI_INVALID_PARAMETER
;
481 if (Event
->Signature
!= EVENT_SIGNATURE
) {
482 return EFI_INVALID_PARAMETER
;
485 CoreAcquireEventLock ();
488 // If the event is not already signalled, do so
491 if (Event
->SignalCount
== 0x00000000) {
492 Event
->SignalCount
++;
495 // If signalling type is a notify function, queue it
497 if ((Event
->Type
& EVT_NOTIFY_SIGNAL
) != 0) {
500 // The CreateEventEx() style requires all members of the Event Group
503 CoreReleaseEventLock ();
504 CoreNotifySignalList (&Event
->EventGroup
);
505 CoreAcquireEventLock ();
507 CoreNotifyEvent (Event
);
512 CoreReleaseEventLock ();
519 Check the status of an event.
521 @param UserEvent The event to check
523 @retval EFI_SUCCESS The event is in the signaled state
524 @retval EFI_NOT_READY The event is not in the signaled state
525 @retval EFI_INVALID_PARAMETER Event is of type EVT_NOTIFY_SIGNAL
531 IN EFI_EVENT UserEvent
540 return EFI_INVALID_PARAMETER
;
543 if (Event
->Signature
!= EVENT_SIGNATURE
) {
544 return EFI_INVALID_PARAMETER
;
547 if ((Event
->Type
& EVT_NOTIFY_SIGNAL
) != 0) {
548 return EFI_INVALID_PARAMETER
;
551 Status
= EFI_NOT_READY
;
553 if ((Event
->SignalCount
== 0) && ((Event
->Type
& EVT_NOTIFY_WAIT
) != 0)) {
556 // Queue the wait notify function
558 CoreAcquireEventLock ();
559 if (Event
->SignalCount
== 0) {
560 CoreNotifyEvent (Event
);
562 CoreReleaseEventLock ();
566 // If the even looks signalled, get the lock and clear it
569 if (Event
->SignalCount
!= 0) {
570 CoreAcquireEventLock ();
572 if (Event
->SignalCount
!= 0) {
573 Event
->SignalCount
= 0;
574 Status
= EFI_SUCCESS
;
577 CoreReleaseEventLock ();
586 Stops execution until an event is signaled.
588 @param NumberOfEvents The number of events in the UserEvents array
589 @param UserEvents An array of EFI_EVENT
590 @param UserIndex Pointer to the index of the event which
591 satisfied the wait condition
593 @retval EFI_SUCCESS The event indicated by Index was signaled.
594 @retval EFI_INVALID_PARAMETER The event indicated by Index has a notification
595 function or Event was not a valid type
596 @retval EFI_UNSUPPORTED The current TPL is not TPL_APPLICATION
602 IN UINTN NumberOfEvents
,
603 IN EFI_EVENT
*UserEvents
,
611 // Can only WaitForEvent at TPL_APPLICATION
613 if (gEfiCurrentTpl
!= TPL_APPLICATION
) {
614 return EFI_UNSUPPORTED
;
619 for(Index
= 0; Index
< NumberOfEvents
; Index
++) {
621 Status
= CoreCheckEvent (UserEvents
[Index
]);
624 // provide index of event that caused problem
626 if (Status
!= EFI_NOT_READY
) {
633 // This was the location of the Idle loop callback in EFI 1.x reference
634 // code. We don't have that concept in this base at this point.
641 Closes an event and frees the event structure.
643 @param UserEvent Event to close
645 @retval EFI_INVALID_PARAMETER Parameters are not valid.
646 @retval EFI_SUCCESS The event has been closed
652 IN EFI_EVENT UserEvent
661 return EFI_INVALID_PARAMETER
;
664 if (Event
->Signature
!= EVENT_SIGNATURE
) {
665 return EFI_INVALID_PARAMETER
;
669 // If it's a timer event, make sure it's not pending
671 if ((Event
->Type
& EVT_TIMER
) != 0) {
672 CoreSetTimer (Event
, TimerCancel
, 0);
675 CoreAcquireEventLock ();
678 // If the event is queued somewhere, remove it
681 if (Event
->RuntimeData
.Link
.ForwardLink
!= NULL
) {
682 RemoveEntryList (&Event
->RuntimeData
.Link
);
685 if (Event
->NotifyLink
.ForwardLink
!= NULL
) {
686 RemoveEntryList (&Event
->NotifyLink
);
689 if (Event
->SignalLink
.ForwardLink
!= NULL
) {
690 RemoveEntryList (&Event
->SignalLink
);
693 CoreReleaseEventLock ();
696 // If the event is registered on a protocol notify, then remove it from the protocol database
698 CoreUnregisterProtocolNotify (Event
);
700 Status
= CoreFreePool (Event
);
701 ASSERT_EFI_ERROR (Status
);