2 Report Status Code Router Driver which produces Report Stataus Code Handler Protocol
3 and Status Code Runtime Protocol.
5 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "ReportStatusCodeRouterRuntimeDxe.h"
12 EFI_HANDLE mHandle
= NULL
;
13 LIST_ENTRY mCallbackListHead
= INITIALIZE_LIST_HEAD_VARIABLE (mCallbackListHead
);
14 EFI_EVENT mVirtualAddressChangeEvent
= NULL
;
17 // Report operation nest status.
18 // If it is set, then the report operation has nested.
20 UINT32 mStatusCodeNestStatus
= 0;
22 EFI_STATUS_CODE_PROTOCOL mStatusCodeProtocol
= {
26 EFI_RSC_HANDLER_PROTOCOL mRscHandlerProtocol
= {
32 Event callback function to invoke status code handler in list.
34 @param Event Event whose notification function is being invoked.
35 @param Context Pointer to the notification function's context, which is
36 always zero in current implementation.
41 RscHandlerNotification (
46 RSC_HANDLER_CALLBACK_ENTRY
*CallbackEntry
;
47 EFI_PHYSICAL_ADDRESS Address
;
48 RSC_DATA_ENTRY
*RscData
;
50 CallbackEntry
= (RSC_HANDLER_CALLBACK_ENTRY
*) Context
;
53 // Traverse the status code data buffer to parse all
56 Address
= CallbackEntry
->StatusCodeDataBuffer
;
57 while (Address
< CallbackEntry
->EndPointer
) {
58 RscData
= (RSC_DATA_ENTRY
*) (UINTN
) Address
;
59 CallbackEntry
->RscHandlerCallback (
67 Address
+= (sizeof (RSC_DATA_ENTRY
) + RscData
->Data
.Size
);
68 Address
= ALIGN_VARIABLE (Address
);
71 CallbackEntry
->EndPointer
= CallbackEntry
->StatusCodeDataBuffer
;
75 Register the callback function for ReportStatusCode() notification.
77 When this function is called the function pointer is added to an internal list and any future calls to
78 ReportStatusCode() will be forwarded to the Callback function. During the bootservices,
79 this is the callback for which this service can be invoked. The report status code router
80 will create an event such that the callback function is only invoked at the TPL for which it was
81 registered. The entity that registers for the callback should also register for an event upon
82 generation of exit boot services and invoke the unregister service.
83 If the handler does not have a TPL dependency, it should register for a callback at TPL high. The
84 router infrastructure will support making callbacks at runtime, but the caller for runtime invocation
85 must meet the following criteria:
86 1. must be a runtime driver type so that its memory is not reclaimed
87 2. not unregister at exit boot services so that the router will still have its callback address
88 3. the caller must be self-contained (eg. Not call out into any boot-service interfaces) and be
89 runtime safe, in general.
91 @param[in] Callback A pointer to a function of type EFI_RSC_HANDLER_CALLBACK that is called when
92 a call to ReportStatusCode() occurs.
93 @param[in] Tpl TPL at which callback can be safely invoked.
95 @retval EFI_SUCCESS Function was successfully registered.
96 @retval EFI_INVALID_PARAMETER The callback function was NULL.
97 @retval EFI_OUT_OF_RESOURCES The internal buffer ran out of space. No more functions can be
99 @retval EFI_ALREADY_STARTED The function was already registered. It can't be registered again.
105 IN EFI_RSC_HANDLER_CALLBACK Callback
,
111 RSC_HANDLER_CALLBACK_ENTRY
*CallbackEntry
;
113 if (Callback
== NULL
) {
114 return EFI_INVALID_PARAMETER
;
117 for (Link
= GetFirstNode (&mCallbackListHead
); !IsNull (&mCallbackListHead
, Link
); Link
= GetNextNode (&mCallbackListHead
, Link
)) {
118 CallbackEntry
= CR (Link
, RSC_HANDLER_CALLBACK_ENTRY
, Node
, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE
);
119 if (CallbackEntry
->RscHandlerCallback
== Callback
) {
121 // If the function was already registered. It can't be registered again.
123 return EFI_ALREADY_STARTED
;
127 CallbackEntry
= AllocateRuntimeZeroPool (sizeof (RSC_HANDLER_CALLBACK_ENTRY
));
128 ASSERT (CallbackEntry
!= NULL
);
130 CallbackEntry
->Signature
= RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE
;
131 CallbackEntry
->RscHandlerCallback
= Callback
;
132 CallbackEntry
->Tpl
= Tpl
;
135 // If TPL of registered callback funtion is not TPL_HIGH_LEVEL, then event should be created
136 // for it, and related buffer for status code data should be prepared.
137 // Here the data buffer must be prepared in advance, because Report Status Code Protocol might
138 // be invoked under TPL_HIGH_LEVEL and no memory allocation is allowed then.
139 // If TPL is TPL_HIGH_LEVEL, then all status code will be reported immediately, without data
140 // buffer and event trigger.
142 if (Tpl
!= TPL_HIGH_LEVEL
) {
143 CallbackEntry
->StatusCodeDataBuffer
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) AllocatePool (EFI_PAGE_SIZE
);
144 CallbackEntry
->BufferSize
= EFI_PAGE_SIZE
;
145 CallbackEntry
->EndPointer
= CallbackEntry
->StatusCodeDataBuffer
;
146 Status
= gBS
->CreateEvent (
149 RscHandlerNotification
,
151 &CallbackEntry
->Event
153 ASSERT_EFI_ERROR (Status
);
156 InsertTailList (&mCallbackListHead
, &CallbackEntry
->Node
);
162 Remove a previously registered callback function from the notification list.
164 A callback function must be unregistered before it is deallocated. It is important that any registered
165 callbacks that are not runtime complaint be unregistered when ExitBootServices() is called.
167 @param[in] Callback A pointer to a function of type EFI_RSC_HANDLER_CALLBACK that is to be
170 @retval EFI_SUCCESS The function was successfully unregistered.
171 @retval EFI_INVALID_PARAMETER The callback function was NULL.
172 @retval EFI_NOT_FOUND The callback function was not found to be unregistered.
178 IN EFI_RSC_HANDLER_CALLBACK Callback
182 RSC_HANDLER_CALLBACK_ENTRY
*CallbackEntry
;
184 if (Callback
== NULL
) {
185 return EFI_INVALID_PARAMETER
;
188 for (Link
= GetFirstNode (&mCallbackListHead
); !IsNull (&mCallbackListHead
, Link
); Link
= GetNextNode (&mCallbackListHead
, Link
)) {
189 CallbackEntry
= CR (Link
, RSC_HANDLER_CALLBACK_ENTRY
, Node
, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE
);
190 if (CallbackEntry
->RscHandlerCallback
== Callback
) {
192 // If the function is found in list, delete it and return.
194 if (CallbackEntry
->Tpl
!= TPL_HIGH_LEVEL
) {
195 FreePool ((VOID
*) (UINTN
) CallbackEntry
->StatusCodeDataBuffer
);
196 gBS
->CloseEvent (CallbackEntry
->Event
);
198 RemoveEntryList (&CallbackEntry
->Node
);
199 FreePool (CallbackEntry
);
204 return EFI_NOT_FOUND
;
208 Provides an interface that a software module can call to report a status code.
210 @param Type Indicates the type of status code being reported.
211 @param Value Describes the current status of a hardware or software entity.
212 This included information about the class and subclass that is used to
213 classify the entity as well as an operation.
214 @param Instance The enumeration of a hardware or software entity within
215 the system. Valid instance numbers start with 1.
216 @param CallerId This optional parameter may be used to identify the caller.
217 This parameter allows the status code driver to apply different rules to
219 @param Data This optional parameter may be used to pass additional data.
221 @retval EFI_SUCCESS The function completed successfully
222 @retval EFI_DEVICE_ERROR The function should not be completed due to a device error.
228 IN EFI_STATUS_CODE_TYPE Type
,
229 IN EFI_STATUS_CODE_VALUE Value
,
231 IN EFI_GUID
*CallerId OPTIONAL
,
232 IN EFI_STATUS_CODE_DATA
*Data OPTIONAL
236 RSC_HANDLER_CALLBACK_ENTRY
*CallbackEntry
;
237 RSC_DATA_ENTRY
*RscData
;
242 // Use atom operation to avoid the reentant of report.
243 // If current status is not zero, then the function is reentrancy.
245 if (InterlockedCompareExchange32 (&mStatusCodeNestStatus
, 0, 1) == 1) {
246 return EFI_DEVICE_ERROR
;
249 for (Link
= GetFirstNode (&mCallbackListHead
); !IsNull (&mCallbackListHead
, Link
);) {
250 CallbackEntry
= CR (Link
, RSC_HANDLER_CALLBACK_ENTRY
, Node
, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE
);
252 // The handler may remove itself, so get the next handler in advance.
254 Link
= GetNextNode (&mCallbackListHead
, Link
);
255 if ((CallbackEntry
->Tpl
== TPL_HIGH_LEVEL
) || EfiAtRuntime ()) {
256 CallbackEntry
->RscHandlerCallback (
267 // If callback is registered with TPL lower than TPL_HIGH_LEVEL, event must be signaled at boot time to possibly wait for
268 // allowed TPL to report status code. Related data should also be stored in data buffer.
270 CallbackEntry
->EndPointer
= ALIGN_VARIABLE (CallbackEntry
->EndPointer
);
271 RscData
= (RSC_DATA_ENTRY
*) (UINTN
) CallbackEntry
->EndPointer
;
272 CallbackEntry
->EndPointer
+= sizeof (RSC_DATA_ENTRY
);
274 CallbackEntry
->EndPointer
+= Data
->Size
;
278 // 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.
280 if (CallbackEntry
->EndPointer
> (CallbackEntry
->StatusCodeDataBuffer
+ (CallbackEntry
->BufferSize
/ 8) * 7)) {
281 if (EfiGetCurrentTpl () < TPL_HIGH_LEVEL
) {
282 NewBuffer
= ReallocatePool (
283 CallbackEntry
->BufferSize
,
284 CallbackEntry
->BufferSize
* 2,
285 (VOID
*) (UINTN
) CallbackEntry
->StatusCodeDataBuffer
287 if (NewBuffer
!= NULL
) {
288 CallbackEntry
->EndPointer
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NewBuffer
+ (CallbackEntry
->EndPointer
- CallbackEntry
->StatusCodeDataBuffer
);
289 CallbackEntry
->StatusCodeDataBuffer
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NewBuffer
;
290 CallbackEntry
->BufferSize
*= 2;
296 // If data buffer is used up, do not report for this time.
298 if (CallbackEntry
->EndPointer
> (CallbackEntry
->StatusCodeDataBuffer
+ CallbackEntry
->BufferSize
)) {
302 RscData
->Type
= Type
;
303 RscData
->Value
= Value
;
304 RscData
->Instance
= Instance
;
305 if (CallerId
!= NULL
) {
306 CopyGuid (&RscData
->CallerId
, CallerId
);
309 CopyMem (&RscData
->Data
, Data
, Data
->HeaderSize
+ Data
->Size
);
312 Status
= gBS
->SignalEvent (CallbackEntry
->Event
);
313 ASSERT_EFI_ERROR (Status
);
317 // Restore the nest status of report
319 InterlockedCompareExchange32 (&mStatusCodeNestStatus
, 1, 0);
325 Virtual address change notification call back. It converts global pointer
328 @param Event Event whose notification function is being invoked.
329 @param Context Pointer to the notification function's context, which is
330 always zero in current implementation.
335 VirtualAddressChangeCallBack (
342 RSC_HANDLER_CALLBACK_ENTRY
*CallbackEntry
;
344 for (Link
= GetFirstNode (&mCallbackListHead
); !IsNull (&mCallbackListHead
, Link
); Link
= GetNextNode (&mCallbackListHead
, Link
)) {
345 CallbackEntry
= CR (Link
, RSC_HANDLER_CALLBACK_ENTRY
, Node
, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE
);
346 Status
= EfiConvertFunctionPointer (0, (VOID
**) &CallbackEntry
->RscHandlerCallback
);
347 ASSERT_EFI_ERROR (Status
);
350 Status
= EfiConvertList (
354 ASSERT_EFI_ERROR (Status
);
358 Entry point of Generic Status Code Driver.
360 This function is the entry point of this Generic Status Code Driver.
361 It installs eport Stataus Code Handler Protocol and Status Code Runtime Protocol,
362 and registers event for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
364 @param ImageHandle The firmware allocated handle for the EFI image.
365 @param SystemTable A pointer to the EFI System Table.
367 @retval EFI_SUCCESS The entry point is executed successfully.
372 GenericStatusCodeRuntimeDxeEntry (
373 IN EFI_HANDLE ImageHandle
,
374 IN EFI_SYSTEM_TABLE
*SystemTable
379 Status
= gBS
->InstallMultipleProtocolInterfaces (
381 &gEfiRscHandlerProtocolGuid
,
382 &mRscHandlerProtocol
,
383 &gEfiStatusCodeRuntimeProtocolGuid
,
384 &mStatusCodeProtocol
,
387 ASSERT_EFI_ERROR (Status
);
389 Status
= gBS
->CreateEventEx (
392 VirtualAddressChangeCallBack
,
394 &gEfiEventVirtualAddressChangeGuid
,
395 &mVirtualAddressChangeEvent
397 ASSERT_EFI_ERROR (Status
);