2 UEFI Event support functions implemented in this file.
4 Copyright (c) 2006 - 2011, 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 queues
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
,
91 /// gIdleLoopEvent - Event which is signalled when the core is idle
93 EFI_EVENT gIdleLoopEvent
= NULL
;
97 Enter critical section by acquiring the lock on gEventQueueLock.
101 CoreAcquireEventLock (
105 CoreAcquireLock (&gEventQueueLock
);
110 Exit critical section by releasing the lock on gEventQueueLock.
114 CoreReleaseEventLock (
118 CoreReleaseLock (&gEventQueueLock
);
124 Initializes "event" support.
126 @retval EFI_SUCCESS Always return success
130 CoreInitializeEventServices (
136 for (Index
=0; Index
<= TPL_HIGH_LEVEL
; Index
++) {
137 InitializeListHead (&gEventQueue
[Index
]);
140 CoreInitializeTimer ();
145 CoreEmptyCallbackFunction
,
157 Dispatches all pending events.
159 @param Priority The task priority level of event notifications
164 CoreDispatchEventNotifies (
171 CoreAcquireEventLock ();
172 ASSERT (gEventQueueLock
.OwnerTpl
== Priority
);
173 Head
= &gEventQueue
[Priority
];
176 // Dispatch all the pending notifications
178 while (!IsListEmpty (Head
)) {
180 Event
= CR (Head
->ForwardLink
, IEVENT
, NotifyLink
, EVENT_SIGNATURE
);
181 RemoveEntryList (&Event
->NotifyLink
);
183 Event
->NotifyLink
.ForwardLink
= NULL
;
186 // Only clear the SIGNAL status if it is a SIGNAL type event.
187 // WAIT type events are only cleared in CheckEvent()
189 if ((Event
->Type
& EVT_NOTIFY_SIGNAL
) != 0) {
190 Event
->SignalCount
= 0;
193 CoreReleaseEventLock ();
198 ASSERT (Event
->NotifyFunction
!= NULL
);
199 Event
->NotifyFunction (Event
, Event
->NotifyContext
);
202 // Check for next pending event
204 CoreAcquireEventLock ();
207 gEventPending
&= ~(1 << Priority
);
208 CoreReleaseEventLock ();
214 Queues the event's notification function to fire.
216 @param Event The Event to notify
226 // Event database must be locked
228 ASSERT_LOCKED (&gEventQueueLock
);
231 // If the event is queued somewhere, remove it
234 if (Event
->NotifyLink
.ForwardLink
!= NULL
) {
235 RemoveEntryList (&Event
->NotifyLink
);
236 Event
->NotifyLink
.ForwardLink
= NULL
;
240 // Queue the event to the pending notification list
243 InsertTailList (&gEventQueue
[Event
->NotifyTpl
], &Event
->NotifyLink
);
244 gEventPending
|= (UINTN
)(1 << Event
->NotifyTpl
);
251 Signals all events in the EventGroup.
253 @param EventGroup The list to signal
257 CoreNotifySignalList (
258 IN EFI_GUID
*EventGroup
265 CoreAcquireEventLock ();
267 Head
= &gEventSignalQueue
;
268 for (Link
= Head
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
269 Event
= CR (Link
, IEVENT
, SignalLink
, EVENT_SIGNATURE
);
270 if (CompareGuid (&Event
->EventGroup
, EventGroup
)) {
271 CoreNotifyEvent (Event
);
275 CoreReleaseEventLock ();
280 Creates a general-purpose event structure.
282 @param Type The type of event to create and its mode and
284 @param NotifyTpl The task priority level of event notifications
285 @param NotifyFunction Pointer to the events notification function
286 @param NotifyContext Pointer to the notification functions context;
287 corresponds to parameter "Context" in the
288 notification function
289 @param Event Pointer to the newly created event if the call
290 succeeds; undefined otherwise
292 @retval EFI_SUCCESS The event structure was created
293 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value
294 @retval EFI_OUT_OF_RESOURCES The event could not be allocated
301 IN EFI_TPL NotifyTpl
,
302 IN EFI_EVENT_NOTIFY NotifyFunction
, OPTIONAL
303 IN VOID
*NotifyContext
, OPTIONAL
307 return CoreCreateEventEx (Type
, NotifyTpl
, NotifyFunction
, NotifyContext
, NULL
, Event
);
313 Creates a general-purpose event structure
315 @param Type The type of event to create and its mode and
317 @param NotifyTpl The task priority level of event notifications
318 @param NotifyFunction Pointer to the events notification function
319 @param NotifyContext Pointer to the notification functions context;
320 corresponds to parameter "Context" in the
321 notification function
322 @param EventGroup GUID for EventGroup if NULL act the same as
324 @param Event Pointer to the newly created event if the call
325 succeeds; undefined otherwise
327 @retval EFI_SUCCESS The event structure was created
328 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value
329 @retval EFI_OUT_OF_RESOURCES The event could not be allocated
336 IN EFI_TPL NotifyTpl
,
337 IN EFI_EVENT_NOTIFY NotifyFunction
, OPTIONAL
338 IN CONST VOID
*NotifyContext
, OPTIONAL
339 IN CONST EFI_GUID
*EventGroup
, OPTIONAL
349 return EFI_INVALID_PARAMETER
;
353 // Check to make sure no reserved flags are set
355 Status
= EFI_INVALID_PARAMETER
;
356 for (Index
= 0; Index
< (sizeof (mEventTable
) / sizeof (UINT32
)); Index
++) {
357 if (Type
== mEventTable
[Index
]) {
358 Status
= EFI_SUCCESS
;
362 if(EFI_ERROR (Status
)) {
363 return EFI_INVALID_PARAMETER
;
367 // Convert Event type for pre-defined Event groups
369 if (EventGroup
!= NULL
) {
371 // For event group, type EVT_SIGNAL_EXIT_BOOT_SERVICES and EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
374 if ((Type
== EVT_SIGNAL_EXIT_BOOT_SERVICES
) || (Type
== EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
)) {
375 return EFI_INVALID_PARAMETER
;
377 if (CompareGuid (EventGroup
, &gEfiEventExitBootServicesGuid
)) {
378 Type
= EVT_SIGNAL_EXIT_BOOT_SERVICES
;
379 } else if (CompareGuid (EventGroup
, &gEfiEventVirtualAddressChangeGuid
)) {
380 Type
= EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
;
384 // Convert EFI 1.10 Events to their UEFI 2.0 CreateEventEx mapping
386 if (Type
== EVT_SIGNAL_EXIT_BOOT_SERVICES
) {
387 EventGroup
= &gEfiEventExitBootServicesGuid
;
388 } else if (Type
== EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) {
389 EventGroup
= &gEfiEventVirtualAddressChangeGuid
;
394 // If it's a notify type of event, check its parameters
396 if ((Type
& (EVT_NOTIFY_WAIT
| EVT_NOTIFY_SIGNAL
)) != 0) {
398 // Check for an invalid NotifyFunction or NotifyTpl
400 if ((NotifyFunction
== NULL
) ||
401 (NotifyTpl
<= TPL_APPLICATION
) ||
402 (NotifyTpl
>= TPL_HIGH_LEVEL
)) {
403 return EFI_INVALID_PARAMETER
;
408 // No notification needed, zero ignored values
411 NotifyFunction
= NULL
;
412 NotifyContext
= NULL
;
416 // Allocate and initialize a new event structure.
418 if ((Type
& EVT_RUNTIME
) != 0) {
419 IEvent
= AllocateRuntimeZeroPool (sizeof (IEVENT
));
421 IEvent
= AllocateZeroPool (sizeof (IEVENT
));
423 if (IEvent
== NULL
) {
424 return EFI_OUT_OF_RESOURCES
;
427 IEvent
->Signature
= EVENT_SIGNATURE
;
430 IEvent
->NotifyTpl
= NotifyTpl
;
431 IEvent
->NotifyFunction
= NotifyFunction
;
432 IEvent
->NotifyContext
= (VOID
*)NotifyContext
;
433 if (EventGroup
!= NULL
) {
434 CopyGuid (&IEvent
->EventGroup
, EventGroup
);
435 IEvent
->ExFlag
= TRUE
;
440 if ((Type
& EVT_RUNTIME
) != 0) {
442 // Keep a list of all RT events so we can tell the RT AP.
444 IEvent
->RuntimeData
.Type
= Type
;
445 IEvent
->RuntimeData
.NotifyTpl
= NotifyTpl
;
446 IEvent
->RuntimeData
.NotifyFunction
= NotifyFunction
;
447 IEvent
->RuntimeData
.NotifyContext
= (VOID
*) NotifyContext
;
448 IEvent
->RuntimeData
.Event
= (EFI_EVENT
*) IEvent
;
449 InsertTailList (&gRuntime
->EventHead
, &IEvent
->RuntimeData
.Link
);
452 CoreAcquireEventLock ();
454 if ((Type
& EVT_NOTIFY_SIGNAL
) != 0x00000000) {
456 // The Event's NotifyFunction must be queued whenever the event is signaled
458 InsertHeadList (&gEventSignalQueue
, &IEvent
->SignalLink
);
461 CoreReleaseEventLock ();
473 Signals the event. Queues the event to be notified if needed.
475 @param UserEvent The event to signal .
477 @retval EFI_INVALID_PARAMETER Parameters are not valid.
478 @retval EFI_SUCCESS The event was signaled.
484 IN EFI_EVENT UserEvent
492 return EFI_INVALID_PARAMETER
;
495 if (Event
->Signature
!= EVENT_SIGNATURE
) {
496 return EFI_INVALID_PARAMETER
;
499 CoreAcquireEventLock ();
502 // If the event is not already signalled, do so
505 if (Event
->SignalCount
== 0x00000000) {
506 Event
->SignalCount
++;
509 // If signalling type is a notify function, queue it
511 if ((Event
->Type
& EVT_NOTIFY_SIGNAL
) != 0) {
514 // The CreateEventEx() style requires all members of the Event Group
517 CoreReleaseEventLock ();
518 CoreNotifySignalList (&Event
->EventGroup
);
519 CoreAcquireEventLock ();
521 CoreNotifyEvent (Event
);
526 CoreReleaseEventLock ();
533 Check the status of an event.
535 @param UserEvent The event to check
537 @retval EFI_SUCCESS The event is in the signaled state
538 @retval EFI_NOT_READY The event is not in the signaled state
539 @retval EFI_INVALID_PARAMETER Event is of type EVT_NOTIFY_SIGNAL
545 IN EFI_EVENT UserEvent
554 return EFI_INVALID_PARAMETER
;
557 if (Event
->Signature
!= EVENT_SIGNATURE
) {
558 return EFI_INVALID_PARAMETER
;
561 if ((Event
->Type
& EVT_NOTIFY_SIGNAL
) != 0) {
562 return EFI_INVALID_PARAMETER
;
565 Status
= EFI_NOT_READY
;
567 if ((Event
->SignalCount
== 0) && ((Event
->Type
& EVT_NOTIFY_WAIT
) != 0)) {
570 // Queue the wait notify function
572 CoreAcquireEventLock ();
573 if (Event
->SignalCount
== 0) {
574 CoreNotifyEvent (Event
);
576 CoreReleaseEventLock ();
580 // If the even looks signalled, get the lock and clear it
583 if (Event
->SignalCount
!= 0) {
584 CoreAcquireEventLock ();
586 if (Event
->SignalCount
!= 0) {
587 Event
->SignalCount
= 0;
588 Status
= EFI_SUCCESS
;
591 CoreReleaseEventLock ();
600 Stops execution until an event is signaled.
602 @param NumberOfEvents The number of events in the UserEvents array
603 @param UserEvents An array of EFI_EVENT
604 @param UserIndex Pointer to the index of the event which
605 satisfied the wait condition
607 @retval EFI_SUCCESS The event indicated by Index was signaled.
608 @retval EFI_INVALID_PARAMETER The event indicated by Index has a notification
609 function or Event was not a valid type
610 @retval EFI_UNSUPPORTED The current TPL is not TPL_APPLICATION
616 IN UINTN NumberOfEvents
,
617 IN EFI_EVENT
*UserEvents
,
625 // Can only WaitForEvent at TPL_APPLICATION
627 if (gEfiCurrentTpl
!= TPL_APPLICATION
) {
628 return EFI_UNSUPPORTED
;
633 for(Index
= 0; Index
< NumberOfEvents
; Index
++) {
635 Status
= CoreCheckEvent (UserEvents
[Index
]);
638 // provide index of event that caused problem
640 if (Status
!= EFI_NOT_READY
) {
647 // Signal the Idle event
649 CoreSignalEvent (gIdleLoopEvent
);
655 Closes an event and frees the event structure.
657 @param UserEvent Event to close
659 @retval EFI_INVALID_PARAMETER Parameters are not valid.
660 @retval EFI_SUCCESS The event has been closed
666 IN EFI_EVENT UserEvent
675 return EFI_INVALID_PARAMETER
;
678 if (Event
->Signature
!= EVENT_SIGNATURE
) {
679 return EFI_INVALID_PARAMETER
;
683 // If it's a timer event, make sure it's not pending
685 if ((Event
->Type
& EVT_TIMER
) != 0) {
686 CoreSetTimer (Event
, TimerCancel
, 0);
689 CoreAcquireEventLock ();
692 // If the event is queued somewhere, remove it
695 if (Event
->RuntimeData
.Link
.ForwardLink
!= NULL
) {
696 RemoveEntryList (&Event
->RuntimeData
.Link
);
699 if (Event
->NotifyLink
.ForwardLink
!= NULL
) {
700 RemoveEntryList (&Event
->NotifyLink
);
703 if (Event
->SignalLink
.ForwardLink
!= NULL
) {
704 RemoveEntryList (&Event
->SignalLink
);
707 CoreReleaseEventLock ();
710 // If the event is registered on a protocol notify, then remove it from the protocol database
712 CoreUnregisterProtocolNotify (Event
);
714 Status
= CoreFreePool (Event
);
715 ASSERT_EFI_ERROR (Status
);