3 Copyright (c) 2006 - 2007, 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 EVT_TIMER
| EVT_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()
45 // 0x00000200 Generic event with a notification function that
46 // is queue when the event is signaled with SignalEvent()
50 // 0x00000201 ExitBootServicesEvent.
52 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
54 // 0x60000202 SetVirtualAddressMapEvent.
56 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
,
59 // 0x00000000 Generic event without a notification function.
60 // It can be signaled with SignalEvent() and checked with CheckEvent()
65 // 0x80000100 Timer event with a notification function that can be
66 // waited on with CheckEvent() or WaitForEvent()
68 EVT_TIMER
| EVT_NOTIFY_WAIT
,
73 CoreAcquireEventLock (
80 Enter critical section by acquiring the lock on gEventQueueLock.
92 CoreAcquireLock (&gEventQueueLock
);
97 CoreReleaseEventLock (
104 Exit critical section by releasing the lock on gEventQueueLock.
116 CoreReleaseLock (&gEventQueueLock
);
121 CoreInitializeEventServices (
128 Initializes "event" support and populates parts of the System and Runtime Table.
136 EFI_SUCCESS - Always return success
142 for (Index
=0; Index
<= TPL_HIGH_LEVEL
; Index
++) {
143 InitializeListHead (&gEventQueue
[Index
]);
146 CoreInitializeTimer ();
153 CoreDispatchEventNotifies (
160 Dispatches all pending events.
164 Priority - The task priority level of event notifications to dispatch
175 CoreAcquireEventLock ();
176 ASSERT (gEventQueueLock
.OwnerTpl
== Priority
);
177 Head
= &gEventQueue
[Priority
];
180 // Dispatch all the pending notifications
182 while (!IsListEmpty (Head
)) {
184 Event
= CR (Head
->ForwardLink
, IEVENT
, NotifyLink
, EVENT_SIGNATURE
);
185 RemoveEntryList (&Event
->NotifyLink
);
187 Event
->NotifyLink
.ForwardLink
= NULL
;
190 // Only clear the SIGNAL status if it is a SIGNAL type event.
191 // WAIT type events are only cleared in CheckEvent()
193 if (Event
->Type
& EVT_NOTIFY_SIGNAL
) {
194 Event
->SignalCount
= 0;
197 CoreReleaseEventLock ();
202 ASSERT (Event
->NotifyFunction
!= NULL
);
203 Event
->NotifyFunction (Event
, Event
->NotifyContext
);
206 // Check for next pending event
208 CoreAcquireEventLock ();
211 gEventPending
&= ~(1 << Priority
);
212 CoreReleaseEventLock ();
225 Queues the event's notification function to fire
229 Event - The Event to notify
239 // Event database must be locked
241 ASSERT_LOCKED (&gEventQueueLock
);
244 // If the event is queued somewhere, remove it
247 if (Event
->NotifyLink
.ForwardLink
!= NULL
) {
248 RemoveEntryList (&Event
->NotifyLink
);
249 Event
->NotifyLink
.ForwardLink
= NULL
;
253 // Queue the event to the pending notification list
256 InsertTailList (&gEventQueue
[Event
->NotifyTpl
], &Event
->NotifyLink
);
257 gEventPending
|= (UINTN
)(1 << Event
->NotifyTpl
);
263 CoreNotifySignalList (
264 IN EFI_GUID
*EventGroup
269 Signals all events in the EventGroup
272 EventGroup - The list to signal
284 CoreAcquireEventLock ();
286 Head
= &gEventSignalQueue
;
287 for (Link
= Head
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
288 Event
= CR (Link
, IEVENT
, SignalLink
, EVENT_SIGNATURE
);
289 if (CompareGuid (&Event
->EventGroup
, EventGroup
)) {
290 CoreNotifyEvent (Event
);
294 CoreReleaseEventLock ();
301 IN EFI_TPL NotifyTpl
,
302 IN EFI_EVENT_NOTIFY NotifyFunction
, OPTIONAL
303 IN VOID
*NotifyContext
, OPTIONAL
309 Creates a general-purpose event structure
312 Type - The type of event to create and its mode and attributes
313 NotifyTpl - The task priority level of event notifications
314 NotifyFunction - Pointer to the events notification function
315 NotifyContext - Pointer to the notification functions context; corresponds to
316 parameter "Context" in the notification function
317 Event - Pointer to the newly created event if the call succeeds; undefined otherwise
320 EFI_SUCCESS - The event structure was created
321 EFI_INVALID_PARAMETER - One of the parameters has an invalid value
322 EFI_OUT_OF_RESOURCES - The event could not be allocated
327 EFI_EVENT_NOTIFY Function
;
330 Function
= NotifyFunction
;
333 // Convert EFI 1.10 Events to thier UEFI 2.0 CreateEventEx mapping
335 if (Type
== EVT_SIGNAL_EXIT_BOOT_SERVICES
) {
336 GuidPtr
= &gEfiEventExitBootServicesGuid
;
337 } else if (Type
== EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) {
338 GuidPtr
= &gEfiEventVirtualAddressChangeGuid
;
341 return CoreCreateEventEx (Type
, NotifyTpl
, Function
, NotifyContext
, GuidPtr
, Event
);
349 IN EFI_TPL NotifyTpl
,
350 IN EFI_EVENT_NOTIFY NotifyFunction
, OPTIONAL
351 IN CONST VOID
*NotifyContext
, OPTIONAL
352 IN CONST EFI_GUID
*EventGroup
, OPTIONAL
358 Creates a general-purpose event structure
361 Type - The type of event to create and its mode and attributes
362 NotifyTpl - The task priority level of event notifications
363 NotifyFunction - Pointer to the events notification function
364 NotifyContext - Pointer to the notification functions context; corresponds to
365 parameter "Context" in the notification function
366 EventGrout - GUID for EventGroup if NULL act the same as gBS->CreateEvent().
367 Event - Pointer to the newly created event if the call succeeds; undefined otherwise
370 EFI_SUCCESS - The event structure was created
371 EFI_INVALID_PARAMETER - One of the parameters has an invalid value
372 EFI_OUT_OF_RESOURCES - The event could not be allocated
381 if ((Event
== NULL
) || (NotifyTpl
== TPL_APPLICATION
)) {
382 return EFI_INVALID_PARAMETER
;
386 // Check to make sure no reserved flags are set
388 Status
= EFI_INVALID_PARAMETER
;
389 for (Index
= 0; Index
< (sizeof (mEventTable
) / sizeof (UINT32
)); Index
++) {
390 if (Type
== mEventTable
[Index
]) {
391 Status
= EFI_SUCCESS
;
395 if(EFI_ERROR (Status
)) {
396 return EFI_INVALID_PARAMETER
;
400 // If it's a notify type of event, check its parameters
402 if ((Type
& (EVT_NOTIFY_WAIT
| EVT_NOTIFY_SIGNAL
))) {
404 // Check for an invalid NotifyFunction or NotifyTpl
406 if ((NotifyFunction
== NULL
) ||
407 (NotifyTpl
< TPL_APPLICATION
) ||
408 (NotifyTpl
>= TPL_HIGH_LEVEL
)) {
409 return EFI_INVALID_PARAMETER
;
414 // No notification needed, zero ignored values
417 NotifyFunction
= NULL
;
418 NotifyContext
= NULL
;
422 // Allcoate and initialize a new event structure.
424 Status
= CoreAllocatePool (
425 (Type
& EVT_RUNTIME
) ? EfiRuntimeServicesData
: EfiBootServicesData
,
429 if (EFI_ERROR (Status
)) {
430 return EFI_OUT_OF_RESOURCES
;
433 SetMem (IEvent
, sizeof (IEVENT
), 0);
435 IEvent
->Signature
= EVENT_SIGNATURE
;
438 IEvent
->NotifyTpl
= NotifyTpl
;
439 IEvent
->NotifyFunction
= NotifyFunction
;
440 IEvent
->NotifyContext
= (VOID
*)NotifyContext
;
441 if (EventGroup
!= NULL
) {
442 CopyGuid (&IEvent
->EventGroup
, EventGroup
);
443 IEvent
->ExFlag
= TRUE
;
448 if (Type
& EVT_RUNTIME
) {
450 // Keep a list of all RT events so we can tell the RT AP.
452 IEvent
->RuntimeData
.Type
= Type
;
453 IEvent
->RuntimeData
.NotifyTpl
= NotifyTpl
;
454 IEvent
->RuntimeData
.NotifyFunction
= NotifyFunction
;
455 IEvent
->RuntimeData
.NotifyContext
= (VOID
*) NotifyContext
;
456 IEvent
->RuntimeData
.Event
= (EFI_EVENT
*) IEvent
;
457 InsertTailList (&gRuntime
->EventHead
, &IEvent
->RuntimeData
.Link
);
460 CoreAcquireEventLock ();
462 if ((Type
& EVT_NOTIFY_SIGNAL
) != 0x00000000) {
464 // The Event's NotifyFunction must be queued whenever the event is signaled
466 InsertHeadList (&gEventSignalQueue
, &IEvent
->SignalLink
);
469 CoreReleaseEventLock ();
482 IN EFI_EVENT UserEvent
488 Signals the event. Queues the event to be notified if needed
492 UserEvent - The event to signal
496 EFI_INVALID_PARAMETER - Parameters are not valid.
498 EFI_SUCCESS - The event was signaled.
507 return EFI_INVALID_PARAMETER
;
510 if (Event
->Signature
!= EVENT_SIGNATURE
) {
511 return EFI_INVALID_PARAMETER
;
514 CoreAcquireEventLock ();
517 // If the event is not already signalled, do so
520 if (Event
->SignalCount
== 0x00000000) {
521 Event
->SignalCount
++;
524 // If signalling type is a notify function, queue it
526 if (Event
->Type
& EVT_NOTIFY_SIGNAL
) {
529 // The CreateEventEx() style requires all members of the Event Group
532 CoreReleaseEventLock ();
533 CoreNotifySignalList (&Event
->EventGroup
);
534 CoreAcquireEventLock ();
536 CoreNotifyEvent (Event
);
541 CoreReleaseEventLock ();
549 IN EFI_EVENT UserEvent
555 Check the status of an event
559 UserEvent - The event to check
563 EFI_SUCCESS - The event is in the signaled state
564 EFI_NOT_READY - The event is not in the signaled state
565 EFI_INVALID_PARAMETER - Event is of type EVT_NOTIFY_SIGNAL
576 return EFI_INVALID_PARAMETER
;
579 if (Event
->Signature
!= EVENT_SIGNATURE
) {
580 return EFI_INVALID_PARAMETER
;
583 if (Event
->Type
& EVT_NOTIFY_SIGNAL
) {
584 return EFI_INVALID_PARAMETER
;
587 Status
= EFI_NOT_READY
;
589 if (!Event
->SignalCount
&& (Event
->Type
& EVT_NOTIFY_WAIT
)) {
592 // Queue the wait notify function
595 CoreAcquireEventLock ();
596 if (!Event
->SignalCount
) {
597 CoreNotifyEvent (Event
);
599 CoreReleaseEventLock ();
603 // If the even looks signalled, get the lock and clear it
606 if (Event
->SignalCount
) {
607 CoreAcquireEventLock ();
609 if (Event
->SignalCount
) {
610 Event
->SignalCount
= 0;
611 Status
= EFI_SUCCESS
;
614 CoreReleaseEventLock ();
625 IN UINTN NumberOfEvents
,
626 IN EFI_EVENT
*UserEvents
,
633 Stops execution until an event is signaled.
637 NumberOfEvents - The number of events in the UserEvents array
638 UserEvents - An array of EFI_EVENT
639 UserIndex - Pointer to the index of the event which satisfied the wait condition
643 EFI_SUCCESS - The event indicated by Index was signaled.
644 EFI_INVALID_PARAMETER - The event indicated by Index has a notification function or
645 Event was not a valid type
646 EFI_UNSUPPORTED - The current TPL is not TPL_APPLICATION
655 // Can only WaitForEvent at TPL_APPLICATION
657 if (gEfiCurrentTpl
!= TPL_APPLICATION
) {
658 return EFI_UNSUPPORTED
;
663 for(Index
= 0; Index
< NumberOfEvents
; Index
++) {
665 Status
= CoreCheckEvent (UserEvents
[Index
]);
668 // provide index of event that caused problem
670 if (Status
!= EFI_NOT_READY
) {
677 // This was the location of the Idle loop callback in EFI 1.x reference
678 // code. We don't have that concept in this base at this point.
687 IN EFI_EVENT UserEvent
693 Closes an event and frees the event structure.
697 UserEvent - Event to close
701 EFI_INVALID_PARAMETER - Parameters are not valid.
703 EFI_SUCCESS - The event has been closed
714 return EFI_INVALID_PARAMETER
;
717 if (Event
->Signature
!= EVENT_SIGNATURE
) {
718 return EFI_INVALID_PARAMETER
;
722 // If it's a timer event, make sure it's not pending
724 if (Event
->Type
& EVT_TIMER
) {
725 CoreSetTimer (Event
, TimerCancel
, 0);
728 CoreAcquireEventLock ();
731 // If the event is queued somewhere, remove it
734 if (Event
->RuntimeData
.Link
.ForwardLink
!= NULL
) {
735 RemoveEntryList (&Event
->RuntimeData
.Link
);
738 if (Event
->NotifyLink
.ForwardLink
!= NULL
) {
739 RemoveEntryList (&Event
->NotifyLink
);
742 if (Event
->SignalLink
.ForwardLink
!= NULL
) {
743 RemoveEntryList (&Event
->SignalLink
);
746 CoreReleaseEventLock ();
749 // If the event is registered on a protocol notify, then remove it from the protocol database
751 CoreUnregisterProtocolNotify (Event
);
753 Status
= CoreFreePool (Event
);
754 ASSERT_EFI_ERROR (Status
);