]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/ReportStatusCodeRouter/RuntimeDxe/ReportStatusCodeRouterRuntimeDxe.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Universal / ReportStatusCodeRouter / RuntimeDxe / ReportStatusCodeRouterRuntimeDxe.c
1 /** @file
2 Report Status Code Router Driver which produces Report Stataus Code Handler Protocol
3 and Status Code Runtime Protocol.
4
5 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "ReportStatusCodeRouterRuntimeDxe.h"
11
12 EFI_HANDLE mHandle = NULL;
13 LIST_ENTRY mCallbackListHead = INITIALIZE_LIST_HEAD_VARIABLE (mCallbackListHead);
14 EFI_EVENT mVirtualAddressChangeEvent = NULL;
15
16 //
17 // Report operation nest status.
18 // If it is set, then the report operation has nested.
19 //
20 UINT32 mStatusCodeNestStatus = 0;
21
22 EFI_STATUS_CODE_PROTOCOL mStatusCodeProtocol = {
23 ReportDispatcher
24 };
25
26 EFI_RSC_HANDLER_PROTOCOL mRscHandlerProtocol = {
27 Register,
28 Unregister
29 };
30
31 /**
32 Event callback function to invoke status code handler in list.
33
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.
37
38 **/
39 VOID
40 EFIAPI
41 RscHandlerNotification (
42 IN EFI_EVENT Event,
43 IN VOID *Context
44 )
45 {
46 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
47 EFI_PHYSICAL_ADDRESS Address;
48 RSC_DATA_ENTRY *RscData;
49
50 CallbackEntry = (RSC_HANDLER_CALLBACK_ENTRY *) Context;
51
52 //
53 // Traverse the status code data buffer to parse all
54 // data to report.
55 //
56 Address = CallbackEntry->StatusCodeDataBuffer;
57 while (Address < CallbackEntry->EndPointer) {
58 RscData = (RSC_DATA_ENTRY *) (UINTN) Address;
59 CallbackEntry->RscHandlerCallback (
60 RscData->Type,
61 RscData->Value,
62 RscData->Instance,
63 &RscData->CallerId,
64 &RscData->Data
65 );
66
67 Address += (sizeof (RSC_DATA_ENTRY) + RscData->Data.Size);
68 Address = ALIGN_VARIABLE (Address);
69 }
70
71 CallbackEntry->EndPointer = CallbackEntry->StatusCodeDataBuffer;
72 }
73
74 /**
75 Register the callback function for ReportStatusCode() notification.
76
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.
90
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.
94
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
98 registered.
99 @retval EFI_ALREADY_STARTED The function was already registered. It can't be registered again.
100
101 **/
102 EFI_STATUS
103 EFIAPI
104 Register (
105 IN EFI_RSC_HANDLER_CALLBACK Callback,
106 IN EFI_TPL Tpl
107 )
108 {
109 EFI_STATUS Status;
110 LIST_ENTRY *Link;
111 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
112
113 if (Callback == NULL) {
114 return EFI_INVALID_PARAMETER;
115 }
116
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) {
120 //
121 // If the function was already registered. It can't be registered again.
122 //
123 return EFI_ALREADY_STARTED;
124 }
125 }
126
127 CallbackEntry = AllocateRuntimeZeroPool (sizeof (RSC_HANDLER_CALLBACK_ENTRY));
128 ASSERT (CallbackEntry != NULL);
129
130 CallbackEntry->Signature = RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE;
131 CallbackEntry->RscHandlerCallback = Callback;
132 CallbackEntry->Tpl = Tpl;
133
134 //
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.
141 //
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 (
147 EVT_NOTIFY_SIGNAL,
148 Tpl,
149 RscHandlerNotification,
150 CallbackEntry,
151 &CallbackEntry->Event
152 );
153 ASSERT_EFI_ERROR (Status);
154 }
155
156 InsertTailList (&mCallbackListHead, &CallbackEntry->Node);
157
158 return EFI_SUCCESS;
159 }
160
161 /**
162 Remove a previously registered callback function from the notification list.
163
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.
166
167 @param[in] Callback A pointer to a function of type EFI_RSC_HANDLER_CALLBACK that is to be
168 unregistered.
169
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.
173
174 **/
175 EFI_STATUS
176 EFIAPI
177 Unregister (
178 IN EFI_RSC_HANDLER_CALLBACK Callback
179 )
180 {
181 LIST_ENTRY *Link;
182 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
183
184 if (Callback == NULL) {
185 return EFI_INVALID_PARAMETER;
186 }
187
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) {
191 //
192 // If the function is found in list, delete it and return.
193 //
194 if (CallbackEntry->Tpl != TPL_HIGH_LEVEL) {
195 FreePool ((VOID *) (UINTN) CallbackEntry->StatusCodeDataBuffer);
196 gBS->CloseEvent (CallbackEntry->Event);
197 }
198 RemoveEntryList (&CallbackEntry->Node);
199 FreePool (CallbackEntry);
200 return EFI_SUCCESS;
201 }
202 }
203
204 return EFI_NOT_FOUND;
205 }
206
207 /**
208 Provides an interface that a software module can call to report a status code.
209
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
218 different callers.
219 @param Data This optional parameter may be used to pass additional data.
220
221 @retval EFI_SUCCESS The function completed successfully
222 @retval EFI_DEVICE_ERROR The function should not be completed due to a device error.
223
224 **/
225 EFI_STATUS
226 EFIAPI
227 ReportDispatcher (
228 IN EFI_STATUS_CODE_TYPE Type,
229 IN EFI_STATUS_CODE_VALUE Value,
230 IN UINT32 Instance,
231 IN EFI_GUID *CallerId OPTIONAL,
232 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
233 )
234 {
235 LIST_ENTRY *Link;
236 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
237 RSC_DATA_ENTRY *RscData;
238 EFI_STATUS Status;
239 VOID *NewBuffer;
240
241 //
242 // Use atom operation to avoid the reentant of report.
243 // If current status is not zero, then the function is reentrancy.
244 //
245 if (InterlockedCompareExchange32 (&mStatusCodeNestStatus, 0, 1) == 1) {
246 return EFI_DEVICE_ERROR;
247 }
248
249 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link);) {
250 CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
251 //
252 // The handler may remove itself, so get the next handler in advance.
253 //
254 Link = GetNextNode (&mCallbackListHead, Link);
255 if ((CallbackEntry->Tpl == TPL_HIGH_LEVEL) || EfiAtRuntime ()) {
256 CallbackEntry->RscHandlerCallback (
257 Type,
258 Value,
259 Instance,
260 CallerId,
261 Data
262 );
263 continue;
264 }
265
266 //
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.
269 //
270 CallbackEntry->EndPointer = ALIGN_VARIABLE (CallbackEntry->EndPointer);
271 RscData = (RSC_DATA_ENTRY *) (UINTN) CallbackEntry->EndPointer;
272 CallbackEntry->EndPointer += sizeof (RSC_DATA_ENTRY);
273 if (Data != NULL) {
274 CallbackEntry->EndPointer += Data->Size;
275 }
276
277 //
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.
279 //
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
286 );
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;
291 }
292 }
293 }
294
295 //
296 // If data buffer is used up, do not report for this time.
297 //
298 if (CallbackEntry->EndPointer > (CallbackEntry->StatusCodeDataBuffer + CallbackEntry->BufferSize)) {
299 continue;
300 }
301
302 RscData->Type = Type;
303 RscData->Value = Value;
304 RscData->Instance = Instance;
305 if (CallerId != NULL) {
306 CopyGuid (&RscData->CallerId, CallerId);
307 }
308 if (Data != NULL) {
309 CopyMem (&RscData->Data, Data, Data->HeaderSize + Data->Size);
310 }
311
312 Status = gBS->SignalEvent (CallbackEntry->Event);
313 ASSERT_EFI_ERROR (Status);
314 }
315
316 //
317 // Restore the nest status of report
318 //
319 InterlockedCompareExchange32 (&mStatusCodeNestStatus, 1, 0);
320
321 return EFI_SUCCESS;
322 }
323
324 /**
325 Virtual address change notification call back. It converts global pointer
326 to virtual address.
327
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.
331
332 **/
333 VOID
334 EFIAPI
335 VirtualAddressChangeCallBack (
336 IN EFI_EVENT Event,
337 IN VOID *Context
338 )
339 {
340 EFI_STATUS Status;
341 LIST_ENTRY *Link;
342 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
343
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);
348 }
349
350 Status = EfiConvertList (
351 0,
352 &mCallbackListHead
353 );
354 ASSERT_EFI_ERROR (Status);
355 }
356
357 /**
358 Entry point of Generic Status Code Driver.
359
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.
363
364 @param ImageHandle The firmware allocated handle for the EFI image.
365 @param SystemTable A pointer to the EFI System Table.
366
367 @retval EFI_SUCCESS The entry point is executed successfully.
368
369 **/
370 EFI_STATUS
371 EFIAPI
372 GenericStatusCodeRuntimeDxeEntry (
373 IN EFI_HANDLE ImageHandle,
374 IN EFI_SYSTEM_TABLE *SystemTable
375 )
376 {
377 EFI_STATUS Status;
378
379 Status = gBS->InstallMultipleProtocolInterfaces (
380 &mHandle,
381 &gEfiRscHandlerProtocolGuid,
382 &mRscHandlerProtocol,
383 &gEfiStatusCodeRuntimeProtocolGuid,
384 &mStatusCodeProtocol,
385 NULL
386 );
387 ASSERT_EFI_ERROR (Status);
388
389 Status = gBS->CreateEventEx (
390 EVT_NOTIFY_SIGNAL,
391 TPL_NOTIFY,
392 VirtualAddressChangeCallBack,
393 NULL,
394 &gEfiEventVirtualAddressChangeGuid,
395 &mVirtualAddressChangeEvent
396 );
397 ASSERT_EFI_ERROR (Status);
398
399 return EFI_SUCCESS;
400 }