]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/ReportStatusCodeRouter/RuntimeDxe/ReportStatusCodeRouterRuntimeDxe.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Universal / ReportStatusCodeRouter / RuntimeDxe / ReportStatusCodeRouterRuntimeDxe.c
CommitLineData
25126a05 1/** @file\r
2 Report Status Code Router Driver which produces Report Stataus Code Handler Protocol\r
3 and Status Code Runtime Protocol.\r
4\r
d1102dba 5 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 6 This program and the accompanying materials\r
25126a05 7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "ReportStatusCodeRouterRuntimeDxe.h"\r
17\r
18EFI_HANDLE mHandle = NULL;\r
19LIST_ENTRY mCallbackListHead = INITIALIZE_LIST_HEAD_VARIABLE (mCallbackListHead);\r
20EFI_EVENT mVirtualAddressChangeEvent = NULL;\r
21\r
22//\r
23// Report operation nest status.\r
24// If it is set, then the report operation has nested.\r
25//\r
26UINT32 mStatusCodeNestStatus = 0;\r
27\r
28EFI_STATUS_CODE_PROTOCOL mStatusCodeProtocol = {\r
29 ReportDispatcher\r
30};\r
31\r
32EFI_RSC_HANDLER_PROTOCOL mRscHandlerProtocol = {\r
33 Register,\r
34 Unregister\r
35 };\r
36\r
cfc2ba61 37/**\r
38 Event callback function to invoke status code handler in list.\r
39\r
40 @param Event Event whose notification function is being invoked.\r
41 @param Context Pointer to the notification function's context, which is\r
42 always zero in current implementation.\r
43\r
44**/\r
45VOID\r
46EFIAPI\r
47RscHandlerNotification (\r
48 IN EFI_EVENT Event,\r
49 IN VOID *Context\r
50 )\r
51{\r
52 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;\r
53 EFI_PHYSICAL_ADDRESS Address;\r
54 RSC_DATA_ENTRY *RscData;\r
55\r
56 CallbackEntry = (RSC_HANDLER_CALLBACK_ENTRY *) Context;\r
57\r
58 //\r
59 // Traverse the status code data buffer to parse all\r
60 // data to report.\r
61 //\r
62 Address = CallbackEntry->StatusCodeDataBuffer;\r
63 while (Address < CallbackEntry->EndPointer) {\r
64 RscData = (RSC_DATA_ENTRY *) (UINTN) Address;\r
65 CallbackEntry->RscHandlerCallback (\r
66 RscData->Type,\r
67 RscData->Value,\r
68 RscData->Instance,\r
69 &RscData->CallerId,\r
70 &RscData->Data\r
71 );\r
72\r
73 Address += (sizeof (RSC_DATA_ENTRY) + RscData->Data.Size);\r
74 Address = ALIGN_VARIABLE (Address);\r
75 }\r
76\r
77 CallbackEntry->EndPointer = CallbackEntry->StatusCodeDataBuffer;\r
78}\r
79\r
25126a05 80/**\r
81 Register the callback function for ReportStatusCode() notification.\r
d1102dba 82\r
25126a05 83 When this function is called the function pointer is added to an internal list and any future calls to\r
84 ReportStatusCode() will be forwarded to the Callback function. During the bootservices,\r
85 this is the callback for which this service can be invoked. The report status code router\r
86 will create an event such that the callback function is only invoked at the TPL for which it was\r
87 registered. The entity that registers for the callback should also register for an event upon\r
88 generation of exit boot services and invoke the unregister service.\r
89 If the handler does not have a TPL dependency, it should register for a callback at TPL high. The\r
90 router infrastructure will support making callbacks at runtime, but the caller for runtime invocation\r
91 must meet the following criteria:\r
92 1. must be a runtime driver type so that its memory is not reclaimed\r
93 2. not unregister at exit boot services so that the router will still have its callback address\r
94 3. the caller must be self-contained (eg. Not call out into any boot-service interfaces) and be\r
95 runtime safe, in general.\r
d1102dba 96\r
25126a05 97 @param[in] Callback A pointer to a function of type EFI_RSC_HANDLER_CALLBACK that is called when\r
98 a call to ReportStatusCode() occurs.\r
d1102dba
LG
99 @param[in] Tpl TPL at which callback can be safely invoked.\r
100\r
25126a05 101 @retval EFI_SUCCESS Function was successfully registered.\r
102 @retval EFI_INVALID_PARAMETER The callback function was NULL.\r
103 @retval EFI_OUT_OF_RESOURCES The internal buffer ran out of space. No more functions can be\r
104 registered.\r
105 @retval EFI_ALREADY_STARTED The function was already registered. It can't be registered again.\r
106\r
107**/\r
108EFI_STATUS\r
109EFIAPI\r
110Register (\r
111 IN EFI_RSC_HANDLER_CALLBACK Callback,\r
112 IN EFI_TPL Tpl\r
113 )\r
114{\r
cfc2ba61 115 EFI_STATUS Status;\r
25126a05 116 LIST_ENTRY *Link;\r
117 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;\r
118\r
119 if (Callback == NULL) {\r
120 return EFI_INVALID_PARAMETER;\r
121 }\r
122\r
123 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {\r
124 CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);\r
125 if (CallbackEntry->RscHandlerCallback == Callback) {\r
126 //\r
127 // If the function was already registered. It can't be registered again.\r
128 //\r
129 return EFI_ALREADY_STARTED;\r
130 }\r
131 }\r
132\r
4e03ff80 133 CallbackEntry = AllocateRuntimeZeroPool (sizeof (RSC_HANDLER_CALLBACK_ENTRY));\r
25126a05 134 ASSERT (CallbackEntry != NULL);\r
135\r
136 CallbackEntry->Signature = RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE;\r
137 CallbackEntry->RscHandlerCallback = Callback;\r
138 CallbackEntry->Tpl = Tpl;\r
139\r
cfc2ba61 140 //\r
141 // If TPL of registered callback funtion is not TPL_HIGH_LEVEL, then event should be created\r
142 // for it, and related buffer for status code data should be prepared.\r
143 // Here the data buffer must be prepared in advance, because Report Status Code Protocol might\r
144 // be invoked under TPL_HIGH_LEVEL and no memory allocation is allowed then.\r
145 // If TPL is TPL_HIGH_LEVEL, then all status code will be reported immediately, without data\r
146 // buffer and event trigger.\r
147 //\r
148 if (Tpl != TPL_HIGH_LEVEL) {\r
43e33207 149 CallbackEntry->StatusCodeDataBuffer = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocatePool (EFI_PAGE_SIZE);\r
cfc2ba61 150 CallbackEntry->BufferSize = EFI_PAGE_SIZE;\r
151 CallbackEntry->EndPointer = CallbackEntry->StatusCodeDataBuffer;\r
152 Status = gBS->CreateEvent (\r
153 EVT_NOTIFY_SIGNAL,\r
154 Tpl,\r
155 RscHandlerNotification,\r
156 CallbackEntry,\r
157 &CallbackEntry->Event\r
158 );\r
159 ASSERT_EFI_ERROR (Status);\r
160 }\r
161\r
25126a05 162 InsertTailList (&mCallbackListHead, &CallbackEntry->Node);\r
163\r
164 return EFI_SUCCESS;\r
165}\r
166\r
167/**\r
168 Remove a previously registered callback function from the notification list.\r
d1102dba 169\r
25126a05 170 A callback function must be unregistered before it is deallocated. It is important that any registered\r
171 callbacks that are not runtime complaint be unregistered when ExitBootServices() is called.\r
d1102dba 172\r
25126a05 173 @param[in] Callback A pointer to a function of type EFI_RSC_HANDLER_CALLBACK that is to be\r
174 unregistered.\r
d1102dba 175\r
25126a05 176 @retval EFI_SUCCESS The function was successfully unregistered.\r
177 @retval EFI_INVALID_PARAMETER The callback function was NULL.\r
178 @retval EFI_NOT_FOUND The callback function was not found to be unregistered.\r
179\r
180**/\r
181EFI_STATUS\r
182EFIAPI\r
183Unregister (\r
184 IN EFI_RSC_HANDLER_CALLBACK Callback\r
185 )\r
186{\r
187 LIST_ENTRY *Link;\r
188 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;\r
189\r
190 if (Callback == NULL) {\r
191 return EFI_INVALID_PARAMETER;\r
192 }\r
193\r
194 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {\r
195 CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);\r
196 if (CallbackEntry->RscHandlerCallback == Callback) {\r
197 //\r
198 // If the function is found in list, delete it and return.\r
199 //\r
cfc2ba61 200 if (CallbackEntry->Tpl != TPL_HIGH_LEVEL) {\r
201 FreePool ((VOID *) (UINTN) CallbackEntry->StatusCodeDataBuffer);\r
202 gBS->CloseEvent (CallbackEntry->Event);\r
203 }\r
25126a05 204 RemoveEntryList (&CallbackEntry->Node);\r
205 FreePool (CallbackEntry);\r
206 return EFI_SUCCESS;\r
207 }\r
208 }\r
209\r
210 return EFI_NOT_FOUND;\r
211}\r
212\r
25126a05 213/**\r
214 Provides an interface that a software module can call to report a status code.\r
215\r
216 @param Type Indicates the type of status code being reported.\r
217 @param Value Describes the current status of a hardware or software entity.\r
218 This included information about the class and subclass that is used to\r
219 classify the entity as well as an operation.\r
220 @param Instance The enumeration of a hardware or software entity within\r
221 the system. Valid instance numbers start with 1.\r
222 @param CallerId This optional parameter may be used to identify the caller.\r
223 This parameter allows the status code driver to apply different rules to\r
224 different callers.\r
225 @param Data This optional parameter may be used to pass additional data.\r
226\r
227 @retval EFI_SUCCESS The function completed successfully\r
228 @retval EFI_DEVICE_ERROR The function should not be completed due to a device error.\r
229\r
230**/\r
231EFI_STATUS\r
232EFIAPI\r
233ReportDispatcher (\r
234 IN EFI_STATUS_CODE_TYPE Type,\r
235 IN EFI_STATUS_CODE_VALUE Value,\r
236 IN UINT32 Instance,\r
237 IN EFI_GUID *CallerId OPTIONAL,\r
238 IN EFI_STATUS_CODE_DATA *Data OPTIONAL\r
239 )\r
240{\r
241 LIST_ENTRY *Link;\r
242 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;\r
cfc2ba61 243 RSC_DATA_ENTRY *RscData;\r
25126a05 244 EFI_STATUS Status;\r
cfc2ba61 245 VOID *NewBuffer;\r
25126a05 246\r
247 //\r
248 // Use atom operation to avoid the reentant of report.\r
249 // If current status is not zero, then the function is reentrancy.\r
250 //\r
251 if (InterlockedCompareExchange32 (&mStatusCodeNestStatus, 0, 1) == 1) {\r
252 return EFI_DEVICE_ERROR;\r
253 }\r
254\r
04bbf1f3 255 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link);) {\r
25126a05 256 CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);\r
04bbf1f3 257 //\r
258 // The handler may remove itself, so get the next handler in advance.\r
259 //\r
260 Link = GetNextNode (&mCallbackListHead, Link);\r
25126a05 261 if ((CallbackEntry->Tpl == TPL_HIGH_LEVEL) || EfiAtRuntime ()) {\r
262 CallbackEntry->RscHandlerCallback (\r
263 Type,\r
264 Value,\r
265 Instance,\r
266 CallerId,\r
267 Data\r
268 );\r
269 continue;\r
270 }\r
271\r
cfc2ba61 272 //\r
273 // If callback is registered with TPL lower than TPL_HIGH_LEVEL, event must be signaled at boot time to possibly wait for\r
274 // allowed TPL to report status code. Related data should also be stored in data buffer.\r
275 //\r
276 CallbackEntry->EndPointer = ALIGN_VARIABLE (CallbackEntry->EndPointer);\r
277 RscData = (RSC_DATA_ENTRY *) (UINTN) CallbackEntry->EndPointer;\r
278 CallbackEntry->EndPointer += sizeof (RSC_DATA_ENTRY);\r
279 if (Data != NULL) {\r
280 CallbackEntry->EndPointer += Data->Size;\r
281 }\r
25126a05 282\r
cfc2ba61 283 //\r
284 // 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.\r
285 //\r
286 if (CallbackEntry->EndPointer > (CallbackEntry->StatusCodeDataBuffer + (CallbackEntry->BufferSize / 8) * 7)) {\r
287 if (EfiGetCurrentTpl () < TPL_HIGH_LEVEL) {\r
288 NewBuffer = ReallocatePool (\r
289 CallbackEntry->BufferSize,\r
290 CallbackEntry->BufferSize * 2,\r
291 (VOID *) (UINTN) CallbackEntry->StatusCodeDataBuffer\r
292 );\r
293 if (NewBuffer != NULL) {\r
43e33207 294 CallbackEntry->EndPointer = (EFI_PHYSICAL_ADDRESS) (UINTN) NewBuffer + (CallbackEntry->EndPointer - CallbackEntry->StatusCodeDataBuffer);\r
295 CallbackEntry->StatusCodeDataBuffer = (EFI_PHYSICAL_ADDRESS) (UINTN) NewBuffer;\r
cfc2ba61 296 CallbackEntry->BufferSize *= 2;\r
297 }\r
298 }\r
299 }\r
25126a05 300\r
cfc2ba61 301 //\r
302 // If data buffer is used up, do not report for this time.\r
303 //\r
304 if (CallbackEntry->EndPointer > (CallbackEntry->StatusCodeDataBuffer + CallbackEntry->BufferSize)) {\r
305 continue;\r
306 }\r
307\r
308 RscData->Type = Type;\r
309 RscData->Value = Value;\r
310 RscData->Instance = Instance;\r
311 if (CallerId != NULL) {\r
312 CopyGuid (&RscData->CallerId, CallerId);\r
313 }\r
314 if (Data != NULL) {\r
315 CopyMem (&RscData->Data, Data, Data->HeaderSize + Data->Size);\r
316 }\r
25126a05 317\r
cfc2ba61 318 Status = gBS->SignalEvent (CallbackEntry->Event);\r
25126a05 319 ASSERT_EFI_ERROR (Status);\r
320 }\r
321\r
322 //\r
323 // Restore the nest status of report\r
324 //\r
325 InterlockedCompareExchange32 (&mStatusCodeNestStatus, 1, 0);\r
326\r
327 return EFI_SUCCESS;\r
328}\r
329\r
330/**\r
331 Virtual address change notification call back. It converts global pointer\r
332 to virtual address.\r
333\r
334 @param Event Event whose notification function is being invoked.\r
335 @param Context Pointer to the notification function's context, which is\r
336 always zero in current implementation.\r
337\r
338**/\r
339VOID\r
340EFIAPI\r
341VirtualAddressChangeCallBack (\r
342 IN EFI_EVENT Event,\r
343 IN VOID *Context\r
344 )\r
345{\r
d1102dba 346 EFI_STATUS Status;\r
25126a05 347 LIST_ENTRY *Link;\r
348 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;\r
349\r
350 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {\r
351 CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);\r
352 Status = EfiConvertFunctionPointer (0, (VOID **) &CallbackEntry->RscHandlerCallback);\r
353 ASSERT_EFI_ERROR (Status);\r
354 }\r
355\r
356 Status = EfiConvertList (\r
357 0,\r
358 &mCallbackListHead\r
359 );\r
360 ASSERT_EFI_ERROR (Status);\r
361}\r
362\r
363/**\r
364 Entry point of Generic Status Code Driver.\r
365\r
366 This function is the entry point of this Generic Status Code Driver.\r
367 It installs eport Stataus Code Handler Protocol and Status Code Runtime Protocol,\r
368 and registers event for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
369\r
370 @param ImageHandle The firmware allocated handle for the EFI image.\r
371 @param SystemTable A pointer to the EFI System Table.\r
d1102dba 372\r
25126a05 373 @retval EFI_SUCCESS The entry point is executed successfully.\r
374\r
375**/\r
376EFI_STATUS\r
377EFIAPI\r
378GenericStatusCodeRuntimeDxeEntry (\r
379 IN EFI_HANDLE ImageHandle,\r
380 IN EFI_SYSTEM_TABLE *SystemTable\r
381 )\r
382{\r
383 EFI_STATUS Status;\r
384\r
385 Status = gBS->InstallMultipleProtocolInterfaces (\r
386 &mHandle,\r
387 &gEfiRscHandlerProtocolGuid,\r
388 &mRscHandlerProtocol,\r
389 &gEfiStatusCodeRuntimeProtocolGuid,\r
390 &mStatusCodeProtocol,\r
391 NULL\r
392 );\r
393 ASSERT_EFI_ERROR (Status);\r
394\r
395 Status = gBS->CreateEventEx (\r
396 EVT_NOTIFY_SIGNAL,\r
397 TPL_NOTIFY,\r
398 VirtualAddressChangeCallBack,\r
399 NULL,\r
400 &gEfiEventVirtualAddressChangeGuid,\r
401 &mVirtualAddressChangeEvent\r
402 );\r
403 ASSERT_EFI_ERROR (Status);\r
404\r
405 return EFI_SUCCESS;\r
406}\r