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.
26 // Enumerate the valid types
28 UINT32 mEventTable
[] = {
30 // 0x80000200 Timer event with a notification function that is
31 // queue when the event is signaled with SignalEvent()
33 EFI_EVENT_TIMER
| EFI_EVENT_NOTIFY_SIGNAL
,
35 // 0x80000000 Timer event without a notification function. It can be
36 // signaled with SignalEvent() and checked with CheckEvent() or WaitForEvent().
40 // 0x00000100 Generic event with a notification function that
41 // can be waited on with CheckEvent() or WaitForEvent()
43 EFI_EVENT_NOTIFY_WAIT
,
45 // 0x00000200 Generic event with a notification function that
46 // is queue when the event is signaled with SignalEvent()
48 EFI_EVENT_NOTIFY_SIGNAL
,
50 // 0x00000201 ExitBootServicesEvent.
52 EFI_EVENT_SIGNAL_EXIT_BOOT_SERVICES
,
54 // 0x60000202 SetVirtualAddressMapEvent.
56 EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
,
58 #if (EFI_SPECIFICATION_VERSION < 0x00020000)
60 // 0x00000203 ReadyToBootEvent.
62 EFI_EVENT_SIGNAL_READY_TO_BOOT
,
64 // 0x00000204 LegacyBootEvent.
66 EFI_EVENT_SIGNAL_LEGACY_BOOT
,
68 // 0x00000603 Signal all ReadyToBootEvents.
70 EFI_EVENT_NOTIFY_SIGNAL_ALL
| EFI_EVENT_SIGNAL_READY_TO_BOOT
,
72 // 0x00000604 Signal all LegacyBootEvents.
74 EFI_EVENT_NOTIFY_SIGNAL_ALL
| EFI_EVENT_SIGNAL_LEGACY_BOOT
,
78 // 0x00000000 Generic event without a notification function.
79 // It can be signaled with SignalEvent() and checked with CheckEvent()
84 // 0x80000100 Timer event with a notification function that can be
85 // waited on with CheckEvent() or WaitForEvent()
87 EFI_EVENT_TIMER
| EFI_EVENT_NOTIFY_WAIT
,
92 CoreAcquireEventLock (
99 Enter critical section by acquiring the lock on gEventQueueLock.
111 CoreAcquireLock (&gEventQueueLock
);
116 CoreReleaseEventLock (
123 Exit critical section by releasing the lock on gEventQueueLock.
135 CoreReleaseLock (&gEventQueueLock
);
140 CoreInitializeEventServices (
147 Initializes "event" support and populates parts of the System and Runtime Table.
155 EFI_SUCCESS - Always return success
161 for (Index
=0; Index
<= EFI_TPL_HIGH_LEVEL
; Index
++) {
162 InitializeListHead (&gEventQueue
[Index
]);
165 CoreInitializeTimer ();
172 CoreDispatchEventNotifies (
179 Dispatches all pending events.
183 Priority - The task priority level of event notifications to dispatch
194 CoreAcquireEventLock ();
195 ASSERT (gEventQueueLock
.OwnerTpl
== Priority
);
196 Head
= &gEventQueue
[Priority
];
199 // Dispatch all the pending notifications
201 while (!IsListEmpty (Head
)) {
203 Event
= CR (Head
->ForwardLink
, IEVENT
, NotifyLink
, EVENT_SIGNATURE
);
204 RemoveEntryList (&Event
->NotifyLink
);
206 Event
->NotifyLink
.ForwardLink
= NULL
;
209 // Only clear the SIGNAL status if it is a SIGNAL type event.
210 // WAIT type events are only cleared in CheckEvent()
212 if (Event
->Type
& EFI_EVENT_NOTIFY_SIGNAL
) {
213 Event
->SignalCount
= 0;
216 CoreReleaseEventLock ();
221 ASSERT (Event
->NotifyFunction
!= NULL
);
222 Event
->NotifyFunction (Event
, Event
->NotifyContext
);
225 // Check for next pending event
227 CoreAcquireEventLock ();
230 gEventPending
&= ~(1 << Priority
);
231 CoreReleaseEventLock ();
244 Queues the event's notification function to fire
248 Event - The Event to notify
258 // Event database must be locked
260 ASSERT_LOCKED (&gEventQueueLock
);
263 // If the event is queued somewhere, remove it
266 if (Event
->NotifyLink
.ForwardLink
!= NULL
) {
267 RemoveEntryList (&Event
->NotifyLink
);
268 Event
->NotifyLink
.ForwardLink
= NULL
;
272 // Queue the event to the pending notification list
275 InsertTailList (&gEventQueue
[Event
->NotifyTpl
], &Event
->NotifyLink
);
276 gEventPending
|= (UINTN
)(1 << Event
->NotifyTpl
);
282 CoreNotifySignalList (
283 IN EFI_GUID
*EventGroup
288 Signals all events in the EventGroup
291 EventGroup - The list to signal
303 CoreAcquireEventLock ();
305 Head
= &gEventSignalQueue
;
306 for (Link
= Head
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
307 Event
= CR (Link
, IEVENT
, SignalLink
, EVENT_SIGNATURE
);
308 if (CompareGuid (&Event
->EventGroup
, EventGroup
)) {
309 CoreNotifyEvent (Event
);
313 CoreReleaseEventLock ();
317 #if (EFI_SPECIFICATION_VERSION < 0x00020000)
322 EventNotifySignalAllNullEvent (
328 // This null event is a size efficent way to enusre that
329 // EFI_EVENT_NOTIFY_SIGNAL_ALL is error checked correctly.
330 // EFI_EVENT_NOTIFY_SIGNAL_ALL is now mapped into
331 // CreateEventEx() and this function is used to make the
332 // old error checking in CreateEvent() for Tiano extensions
347 IN EFI_TPL NotifyTpl
,
348 IN EFI_EVENT_NOTIFY NotifyFunction
, OPTIONAL
349 IN VOID
*NotifyContext
, OPTIONAL
355 Creates a general-purpose event structure
358 Type - The type of event to create and its mode and attributes
359 NotifyTpl - The task priority level of event notifications
360 NotifyFunction - Pointer to the events notification function
361 NotifyContext - Pointer to the notification functions context; corresponds to
362 parameter "Context" in the notification function
363 Event - Pointer to the newly created event if the call succeeds; undefined otherwise
366 EFI_SUCCESS - The event structure was created
367 EFI_INVALID_PARAMETER - One of the parameters has an invalid value
368 EFI_OUT_OF_RESOURCES - The event could not be allocated
373 EFI_EVENT_NOTIFY Function
;
376 Function
= NotifyFunction
;
378 #if (EFI_SPECIFICATION_VERSION < 0x00020000)
380 // Clear EFI_EVENT_NOFITY_SIGNAL_ALL (Tiano extension) as all events in the
381 // EventGroup now have this property. So we need to filter it out.
383 if (Type
& EFI_EVENT_NOTIFY_SIGNAL_ALL
) {
384 Type
&= ~EFI_EVENT_NOTIFY_SIGNAL_ALL
;
385 Function
= EventNotifySignalAllNullEvent
;
389 // Map the Tiano extensions Events to CreateEventEx form
391 if (Type
== EFI_EVENT_SIGNAL_READY_TO_BOOT
) {
392 GuidPtr
= &gEfiEventReadyToBootGuid
;
393 } else if (Type
== EFI_EVENT_SIGNAL_LEGACY_BOOT
) {
394 GuidPtr
= &gEfiEventLegacyBootGuid
399 // Convert EFI 1.10 Events to thier UEFI 2.0 CreateEventEx mapping
401 if (Type
== EVENT_SIGNAL_EXIT_BOOT_SERVICES
) {
402 GuidPtr
= &gEfiEventExitBootServicesGuid
;
403 } else if (Type
== EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) {
404 GuidPtr
= &gEfiEventVirtualAddressChangeGuid
;
407 return CoreCreateEventEx (Type
, NotifyTpl
, Function
, NotifyContext
, GuidPtr
, Event
);
415 IN EFI_TPL NotifyTpl
,
416 IN EFI_EVENT_NOTIFY NotifyFunction
, OPTIONAL
417 IN CONST VOID
*NotifyContext
, OPTIONAL
418 IN CONST EFI_GUID
*EventGroup
, OPTIONAL
424 Creates a general-purpose event structure
427 Type - The type of event to create and its mode and attributes
428 NotifyTpl - The task priority level of event notifications
429 NotifyFunction - Pointer to the events notification function
430 NotifyContext - Pointer to the notification functions context; corresponds to
431 parameter "Context" in the notification function
432 EventGrout - GUID for EventGroup if NULL act the same as gBS->CreateEvent().
433 Event - Pointer to the newly created event if the call succeeds; undefined otherwise
436 EFI_SUCCESS - The event structure was created
437 EFI_INVALID_PARAMETER - One of the parameters has an invalid value
438 EFI_OUT_OF_RESOURCES - The event could not be allocated
447 if ((Event
== NULL
) || (NotifyTpl
== EFI_TPL_APPLICATION
)) {
448 return EFI_INVALID_PARAMETER
;
452 // Check to make sure no reserved flags are set
454 Status
= EFI_INVALID_PARAMETER
;
455 for (Index
= 0; Index
< (sizeof (mEventTable
) / sizeof (UINT32
)); Index
++) {
456 if (Type
== mEventTable
[Index
]) {
457 Status
= EFI_SUCCESS
;
461 if(EFI_ERROR (Status
)) {
462 return EFI_INVALID_PARAMETER
;
466 // If it's a notify type of event, check its parameters
468 if ((Type
& (EFI_EVENT_NOTIFY_WAIT
| EFI_EVENT_NOTIFY_SIGNAL
))) {
470 // Check for an invalid NotifyFunction or NotifyTpl
472 if ((NotifyFunction
== NULL
) ||
473 (NotifyTpl
< EFI_TPL_APPLICATION
) ||
474 (NotifyTpl
>= EFI_TPL_HIGH_LEVEL
)) {
475 return EFI_INVALID_PARAMETER
;
480 // No notification needed, zero ignored values
483 NotifyFunction
= NULL
;
484 NotifyContext
= NULL
;
488 // Allcoate and initialize a new event structure.
490 Status
= CoreAllocatePool (
491 (Type
& EFI_EVENT_RUNTIME
) ? EfiRuntimeServicesData
: EfiBootServicesData
,
495 if (EFI_ERROR (Status
)) {
496 return EFI_OUT_OF_RESOURCES
;
499 SetMem (IEvent
, sizeof (IEVENT
), 0);
501 IEvent
->Signature
= EVENT_SIGNATURE
;
504 IEvent
->NotifyTpl
= NotifyTpl
;
505 IEvent
->NotifyFunction
= NotifyFunction
;
506 IEvent
->NotifyContext
= (VOID
*)NotifyContext
;
507 if (EventGroup
!= NULL
) {
508 CopyGuid (&IEvent
->EventGroup
, EventGroup
);
509 IEvent
->ExFlag
= TRUE
;
514 if (Type
& EFI_EVENT_RUNTIME
) {
516 // Keep a list of all RT events so we can tell the RT AP.
518 IEvent
->RuntimeData
.Type
= Type
;
519 IEvent
->RuntimeData
.NotifyTpl
= NotifyTpl
;
520 IEvent
->RuntimeData
.NotifyFunction
= NotifyFunction
;
521 IEvent
->RuntimeData
.NotifyContext
= (VOID
*) NotifyContext
;
522 IEvent
->RuntimeData
.Event
= (EFI_EVENT
*) IEvent
;
523 InsertTailList (&gRuntime
->EventHead
, &IEvent
->RuntimeData
.Link
);
526 CoreAcquireEventLock ();
528 if ((Type
& EFI_EVENT_NOTIFY_SIGNAL
) != 0x00000000) {
530 // The Event's NotifyFunction must be queued whenever the event is signaled
532 InsertHeadList (&gEventSignalQueue
, &IEvent
->SignalLink
);
535 CoreReleaseEventLock ();
548 IN EFI_EVENT UserEvent
554 Signals the event. Queues the event to be notified if needed
558 UserEvent - The event to signal
562 EFI_INVALID_PARAMETER - Parameters are not valid.
564 EFI_SUCCESS - The event was signaled.
573 return EFI_INVALID_PARAMETER
;
576 if (Event
->Signature
!= EVENT_SIGNATURE
) {
577 return EFI_INVALID_PARAMETER
;
580 CoreAcquireEventLock ();
583 // If the event is not already signalled, do so
586 if (Event
->SignalCount
== 0x00000000) {
587 Event
->SignalCount
++;
590 // If signalling type is a notify function, queue it
592 if (Event
->Type
& EFI_EVENT_NOTIFY_SIGNAL
) {
595 // The CreateEventEx() style requires all members of the Event Group
598 CoreReleaseEventLock ();
599 CoreNotifySignalList (&Event
->EventGroup
);
600 CoreAcquireEventLock ();
602 CoreNotifyEvent (Event
);
607 CoreReleaseEventLock ();
615 IN EFI_EVENT UserEvent
621 Check the status of an event
625 UserEvent - The event to check
629 EFI_SUCCESS - The event is in the signaled state
630 EFI_NOT_READY - The event is not in the signaled state
631 EFI_INVALID_PARAMETER - Event is of type EVT_NOTIFY_SIGNAL
642 return EFI_INVALID_PARAMETER
;
645 if (Event
->Signature
!= EVENT_SIGNATURE
) {
646 return EFI_INVALID_PARAMETER
;
649 if (Event
->Type
& EFI_EVENT_NOTIFY_SIGNAL
) {
650 return EFI_INVALID_PARAMETER
;
653 Status
= EFI_NOT_READY
;
655 if (!Event
->SignalCount
&& (Event
->Type
& EFI_EVENT_NOTIFY_WAIT
)) {
658 // Queue the wait notify function
661 CoreAcquireEventLock ();
662 if (!Event
->SignalCount
) {
663 CoreNotifyEvent (Event
);
665 CoreReleaseEventLock ();
669 // If the even looks signalled, get the lock and clear it
672 if (Event
->SignalCount
) {
673 CoreAcquireEventLock ();
675 if (Event
->SignalCount
) {
676 Event
->SignalCount
= 0;
677 Status
= EFI_SUCCESS
;
680 CoreReleaseEventLock ();
691 IN UINTN NumberOfEvents
,
692 IN EFI_EVENT
*UserEvents
,
699 Stops execution until an event is signaled.
703 NumberOfEvents - The number of events in the UserEvents array
704 UserEvents - An array of EFI_EVENT
705 UserIndex - Pointer to the index of the event which satisfied the wait condition
709 EFI_SUCCESS - The event indicated by Index was signaled.
710 EFI_INVALID_PARAMETER - The event indicated by Index has a notification function or
711 Event was not a valid type
712 EFI_UNSUPPORTED - The current TPL is not TPL_APPLICATION
721 // Can only WaitForEvent at TPL_APPLICATION
723 if (gEfiCurrentTpl
!= EFI_TPL_APPLICATION
) {
724 return EFI_UNSUPPORTED
;
729 for(Index
= 0; Index
< NumberOfEvents
; Index
++) {
731 Status
= CoreCheckEvent (UserEvents
[Index
]);
734 // provide index of event that caused problem
736 if (Status
!= EFI_NOT_READY
) {
743 // This was the location of the Idle loop callback in EFI 1.x reference
744 // code. We don't have that concept in this base at this point.
753 IN EFI_EVENT UserEvent
759 Closes an event and frees the event structure.
763 UserEvent - Event to close
767 EFI_INVALID_PARAMETER - Parameters are not valid.
769 EFI_SUCCESS - The event has been closed
780 return EFI_INVALID_PARAMETER
;
783 if (Event
->Signature
!= EVENT_SIGNATURE
) {
784 return EFI_INVALID_PARAMETER
;
788 // If it's a timer event, make sure it's not pending
790 if (Event
->Type
& EFI_EVENT_TIMER
) {
791 CoreSetTimer (Event
, TimerCancel
, 0);
794 CoreAcquireEventLock ();
797 // If the event is queued somewhere, remove it
800 if (Event
->RuntimeData
.Link
.ForwardLink
!= NULL
) {
801 RemoveEntryList (&Event
->RuntimeData
.Link
);
804 if (Event
->NotifyLink
.ForwardLink
!= NULL
) {
805 RemoveEntryList (&Event
->NotifyLink
);
808 if (Event
->SignalLink
.ForwardLink
!= NULL
) {
809 RemoveEntryList (&Event
->SignalLink
);
812 CoreReleaseEventLock ();
815 // If the event is registered on a protocol notify, then remove it from the protocol database
817 CoreUnregisterProtocolNotify (Event
);
819 Status
= CoreFreePool (Event
);
820 ASSERT_EFI_ERROR (Status
);