2 Report Status Code Router Driver which produces Report Stataus Code Handler Protocol
3 and Status Code Runtime Protocol.
5 Copyright (c) 2009, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "ReportStatusCodeRouterRuntimeDxe.h"
18 EFI_HANDLE mHandle
= NULL
;
19 LIST_ENTRY mCallbackListHead
= INITIALIZE_LIST_HEAD_VARIABLE (mCallbackListHead
);
20 EFI_EVENT mVirtualAddressChangeEvent
= NULL
;
23 // Report operation nest status.
24 // If it is set, then the report operation has nested.
26 UINT32 mStatusCodeNestStatus
= 0;
28 EFI_STATUS_CODE_PROTOCOL mStatusCodeProtocol
= {
32 EFI_RSC_HANDLER_PROTOCOL mRscHandlerProtocol
= {
38 Event callback function to invoke status code handler in list.
40 @param Event Event whose notification function is being invoked.
41 @param Context Pointer to the notification function's context, which is
42 always zero in current implementation.
47 RscHandlerNotification (
52 RSC_HANDLER_CALLBACK_ENTRY
*CallbackEntry
;
53 EFI_PHYSICAL_ADDRESS Address
;
54 RSC_DATA_ENTRY
*RscData
;
56 CallbackEntry
= (RSC_HANDLER_CALLBACK_ENTRY
*) Context
;
59 // Traverse the status code data buffer to parse all
62 Address
= CallbackEntry
->StatusCodeDataBuffer
;
63 while (Address
< CallbackEntry
->EndPointer
) {
64 RscData
= (RSC_DATA_ENTRY
*) (UINTN
) Address
;
65 CallbackEntry
->RscHandlerCallback (
73 Address
+= (sizeof (RSC_DATA_ENTRY
) + RscData
->Data
.Size
);
74 Address
= ALIGN_VARIABLE (Address
);
77 CallbackEntry
->EndPointer
= CallbackEntry
->StatusCodeDataBuffer
;
81 Register the callback function for ReportStatusCode() notification.
83 When this function is called the function pointer is added to an internal list and any future calls to
84 ReportStatusCode() will be forwarded to the Callback function. During the bootservices,
85 this is the callback for which this service can be invoked. The report status code router
86 will create an event such that the callback function is only invoked at the TPL for which it was
87 registered. The entity that registers for the callback should also register for an event upon
88 generation of exit boot services and invoke the unregister service.
89 If the handler does not have a TPL dependency, it should register for a callback at TPL high. The
90 router infrastructure will support making callbacks at runtime, but the caller for runtime invocation
91 must meet the following criteria:
92 1. must be a runtime driver type so that its memory is not reclaimed
93 2. not unregister at exit boot services so that the router will still have its callback address
94 3. the caller must be self-contained (eg. Not call out into any boot-service interfaces) and be
95 runtime safe, in general.
97 @param[in] Callback A pointer to a function of type EFI_RSC_HANDLER_CALLBACK that is called when
98 a call to ReportStatusCode() occurs.
99 @param[in] Tpl TPL at which callback can be safely invoked.
101 @retval EFI_SUCCESS Function was successfully registered.
102 @retval EFI_INVALID_PARAMETER The callback function was NULL.
103 @retval EFI_OUT_OF_RESOURCES The internal buffer ran out of space. No more functions can be
105 @retval EFI_ALREADY_STARTED The function was already registered. It can't be registered again.
111 IN EFI_RSC_HANDLER_CALLBACK Callback
,
117 RSC_HANDLER_CALLBACK_ENTRY
*CallbackEntry
;
119 if (Callback
== NULL
) {
120 return EFI_INVALID_PARAMETER
;
123 for (Link
= GetFirstNode (&mCallbackListHead
); !IsNull (&mCallbackListHead
, Link
); Link
= GetNextNode (&mCallbackListHead
, Link
)) {
124 CallbackEntry
= CR (Link
, RSC_HANDLER_CALLBACK_ENTRY
, Node
, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE
);
125 if (CallbackEntry
->RscHandlerCallback
== Callback
) {
127 // If the function was already registered. It can't be registered again.
129 return EFI_ALREADY_STARTED
;
133 CallbackEntry
= AllocateZeroPool (sizeof (RSC_HANDLER_CALLBACK_ENTRY
));
134 ASSERT (CallbackEntry
!= NULL
);
136 CallbackEntry
->Signature
= RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE
;
137 CallbackEntry
->RscHandlerCallback
= Callback
;
138 CallbackEntry
->Tpl
= Tpl
;
141 // If TPL of registered callback funtion is not TPL_HIGH_LEVEL, then event should be created
142 // for it, and related buffer for status code data should be prepared.
143 // Here the data buffer must be prepared in advance, because Report Status Code Protocol might
144 // be invoked under TPL_HIGH_LEVEL and no memory allocation is allowed then.
145 // If TPL is TPL_HIGH_LEVEL, then all status code will be reported immediately, without data
146 // buffer and event trigger.
148 if (Tpl
!= TPL_HIGH_LEVEL
) {
149 CallbackEntry
->StatusCodeDataBuffer
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) AllocatePool (EFI_PAGE_SIZE
);
150 CallbackEntry
->BufferSize
= EFI_PAGE_SIZE
;
151 CallbackEntry
->EndPointer
= CallbackEntry
->StatusCodeDataBuffer
;
152 Status
= gBS
->CreateEvent (
155 RscHandlerNotification
,
157 &CallbackEntry
->Event
159 ASSERT_EFI_ERROR (Status
);
162 InsertTailList (&mCallbackListHead
, &CallbackEntry
->Node
);
168 Remove a previously registered callback function from the notification list.
170 A callback function must be unregistered before it is deallocated. It is important that any registered
171 callbacks that are not runtime complaint be unregistered when ExitBootServices() is called.
173 @param[in] Callback A pointer to a function of type EFI_RSC_HANDLER_CALLBACK that is to be
176 @retval EFI_SUCCESS The function was successfully unregistered.
177 @retval EFI_INVALID_PARAMETER The callback function was NULL.
178 @retval EFI_NOT_FOUND The callback function was not found to be unregistered.
184 IN EFI_RSC_HANDLER_CALLBACK Callback
188 RSC_HANDLER_CALLBACK_ENTRY
*CallbackEntry
;
190 if (Callback
== NULL
) {
191 return EFI_INVALID_PARAMETER
;
194 for (Link
= GetFirstNode (&mCallbackListHead
); !IsNull (&mCallbackListHead
, Link
); Link
= GetNextNode (&mCallbackListHead
, Link
)) {
195 CallbackEntry
= CR (Link
, RSC_HANDLER_CALLBACK_ENTRY
, Node
, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE
);
196 if (CallbackEntry
->RscHandlerCallback
== Callback
) {
198 // If the function is found in list, delete it and return.
200 if (CallbackEntry
->Tpl
!= TPL_HIGH_LEVEL
) {
201 FreePool ((VOID
*) (UINTN
) CallbackEntry
->StatusCodeDataBuffer
);
202 gBS
->CloseEvent (CallbackEntry
->Event
);
204 RemoveEntryList (&CallbackEntry
->Node
);
205 FreePool (CallbackEntry
);
210 return EFI_NOT_FOUND
;
214 Provides an interface that a software module can call to report a status code.
216 @param Type Indicates the type of status code being reported.
217 @param Value Describes the current status of a hardware or software entity.
218 This included information about the class and subclass that is used to
219 classify the entity as well as an operation.
220 @param Instance The enumeration of a hardware or software entity within
221 the system. Valid instance numbers start with 1.
222 @param CallerId This optional parameter may be used to identify the caller.
223 This parameter allows the status code driver to apply different rules to
225 @param Data This optional parameter may be used to pass additional data.
227 @retval EFI_SUCCESS The function completed successfully
228 @retval EFI_DEVICE_ERROR The function should not be completed due to a device error.
234 IN EFI_STATUS_CODE_TYPE Type
,
235 IN EFI_STATUS_CODE_VALUE Value
,
237 IN EFI_GUID
*CallerId OPTIONAL
,
238 IN EFI_STATUS_CODE_DATA
*Data OPTIONAL
242 RSC_HANDLER_CALLBACK_ENTRY
*CallbackEntry
;
243 RSC_DATA_ENTRY
*RscData
;
248 // Use atom operation to avoid the reentant of report.
249 // If current status is not zero, then the function is reentrancy.
251 if (InterlockedCompareExchange32 (&mStatusCodeNestStatus
, 0, 1) == 1) {
252 return EFI_DEVICE_ERROR
;
255 for (Link
= GetFirstNode (&mCallbackListHead
); !IsNull (&mCallbackListHead
, Link
); Link
= GetNextNode (&mCallbackListHead
, Link
)) {
256 CallbackEntry
= CR (Link
, RSC_HANDLER_CALLBACK_ENTRY
, Node
, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE
);
258 if ((CallbackEntry
->Tpl
== TPL_HIGH_LEVEL
) || EfiAtRuntime ()) {
259 CallbackEntry
->RscHandlerCallback (
270 // If callback is registered with TPL lower than TPL_HIGH_LEVEL, event must be signaled at boot time to possibly wait for
271 // allowed TPL to report status code. Related data should also be stored in data buffer.
273 CallbackEntry
->EndPointer
= ALIGN_VARIABLE (CallbackEntry
->EndPointer
);
274 RscData
= (RSC_DATA_ENTRY
*) (UINTN
) CallbackEntry
->EndPointer
;
275 CallbackEntry
->EndPointer
+= sizeof (RSC_DATA_ENTRY
);
277 CallbackEntry
->EndPointer
+= Data
->Size
;
281 // If data buffer is about to be used up (7/8 here), try to reallocate a buffer with double size, if not at TPL_HIGH_LEVEL.
283 if (CallbackEntry
->EndPointer
> (CallbackEntry
->StatusCodeDataBuffer
+ (CallbackEntry
->BufferSize
/ 8) * 7)) {
284 if (EfiGetCurrentTpl () < TPL_HIGH_LEVEL
) {
285 NewBuffer
= ReallocatePool (
286 CallbackEntry
->BufferSize
,
287 CallbackEntry
->BufferSize
* 2,
288 (VOID
*) (UINTN
) CallbackEntry
->StatusCodeDataBuffer
290 if (NewBuffer
!= NULL
) {
291 CallbackEntry
->EndPointer
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NewBuffer
+ (CallbackEntry
->EndPointer
- CallbackEntry
->StatusCodeDataBuffer
);
292 CallbackEntry
->StatusCodeDataBuffer
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NewBuffer
;
293 CallbackEntry
->BufferSize
*= 2;
299 // If data buffer is used up, do not report for this time.
301 if (CallbackEntry
->EndPointer
> (CallbackEntry
->StatusCodeDataBuffer
+ CallbackEntry
->BufferSize
)) {
305 RscData
->Type
= Type
;
306 RscData
->Value
= Value
;
307 RscData
->Instance
= Instance
;
308 if (CallerId
!= NULL
) {
309 CopyGuid (&RscData
->CallerId
, CallerId
);
312 CopyMem (&RscData
->Data
, Data
, Data
->HeaderSize
+ Data
->Size
);
315 Status
= gBS
->SignalEvent (CallbackEntry
->Event
);
316 ASSERT_EFI_ERROR (Status
);
320 // Restore the nest status of report
322 InterlockedCompareExchange32 (&mStatusCodeNestStatus
, 1, 0);
328 Virtual address change notification call back. It converts global pointer
331 @param Event Event whose notification function is being invoked.
332 @param Context Pointer to the notification function's context, which is
333 always zero in current implementation.
338 VirtualAddressChangeCallBack (
345 RSC_HANDLER_CALLBACK_ENTRY
*CallbackEntry
;
347 for (Link
= GetFirstNode (&mCallbackListHead
); !IsNull (&mCallbackListHead
, Link
); Link
= GetNextNode (&mCallbackListHead
, Link
)) {
348 CallbackEntry
= CR (Link
, RSC_HANDLER_CALLBACK_ENTRY
, Node
, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE
);
349 Status
= EfiConvertFunctionPointer (0, (VOID
**) &CallbackEntry
->RscHandlerCallback
);
350 ASSERT_EFI_ERROR (Status
);
353 Status
= EfiConvertList (
357 ASSERT_EFI_ERROR (Status
);
361 Entry point of Generic Status Code Driver.
363 This function is the entry point of this Generic Status Code Driver.
364 It installs eport Stataus Code Handler Protocol and Status Code Runtime Protocol,
365 and registers event for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
367 @param ImageHandle The firmware allocated handle for the EFI image.
368 @param SystemTable A pointer to the EFI System Table.
370 @retval EFI_SUCCESS The entry point is executed successfully.
375 GenericStatusCodeRuntimeDxeEntry (
376 IN EFI_HANDLE ImageHandle
,
377 IN EFI_SYSTEM_TABLE
*SystemTable
382 Status
= gBS
->InstallMultipleProtocolInterfaces (
384 &gEfiRscHandlerProtocolGuid
,
385 &mRscHandlerProtocol
,
386 &gEfiStatusCodeRuntimeProtocolGuid
,
387 &mStatusCodeProtocol
,
390 ASSERT_EFI_ERROR (Status
);
392 Status
= gBS
->CreateEventEx (
395 VirtualAddressChangeCallBack
,
397 &gEfiEventVirtualAddressChangeGuid
,
398 &mVirtualAddressChangeEvent
400 ASSERT_EFI_ERROR (Status
);