]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/ReportStatusCodeRouter/RuntimeDxe/ReportStatusCodeRouterRuntimeDxe.c
SMM driver should link UefiRuntimeLib.
[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
5 Copyright (c) 2009, Intel Corporation\r
6 All rights reserved. This program and the accompanying materials\r
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
82 \r
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
96 \r
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
99 @param[in] Tpl TPL at which callback can be safely invoked. \r
100 \r
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
cfc2ba61 133 CallbackEntry = AllocateZeroPool (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
169 \r
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
172 \r
173 @param[in] Callback A pointer to a function of type EFI_RSC_HANDLER_CALLBACK that is to be\r
174 unregistered.\r
175 \r
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
255 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {\r
256 CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);\r
257\r
258 if ((CallbackEntry->Tpl == TPL_HIGH_LEVEL) || EfiAtRuntime ()) {\r
259 CallbackEntry->RscHandlerCallback (\r
260 Type,\r
261 Value,\r
262 Instance,\r
263 CallerId,\r
264 Data\r
265 );\r
266 continue;\r
267 }\r
268\r
cfc2ba61 269 //\r
270 // If callback is registered with TPL lower than TPL_HIGH_LEVEL, event must be signaled at boot time to possibly wait for\r
271 // allowed TPL to report status code. Related data should also be stored in data buffer.\r
272 //\r
273 CallbackEntry->EndPointer = ALIGN_VARIABLE (CallbackEntry->EndPointer);\r
274 RscData = (RSC_DATA_ENTRY *) (UINTN) CallbackEntry->EndPointer;\r
275 CallbackEntry->EndPointer += sizeof (RSC_DATA_ENTRY);\r
276 if (Data != NULL) {\r
277 CallbackEntry->EndPointer += Data->Size;\r
278 }\r
25126a05 279\r
cfc2ba61 280 //\r
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.\r
282 //\r
283 if (CallbackEntry->EndPointer > (CallbackEntry->StatusCodeDataBuffer + (CallbackEntry->BufferSize / 8) * 7)) {\r
284 if (EfiGetCurrentTpl () < TPL_HIGH_LEVEL) {\r
285 NewBuffer = ReallocatePool (\r
286 CallbackEntry->BufferSize,\r
287 CallbackEntry->BufferSize * 2,\r
288 (VOID *) (UINTN) CallbackEntry->StatusCodeDataBuffer\r
289 );\r
290 if (NewBuffer != NULL) {\r
43e33207 291 CallbackEntry->EndPointer = (EFI_PHYSICAL_ADDRESS) (UINTN) NewBuffer + (CallbackEntry->EndPointer - CallbackEntry->StatusCodeDataBuffer);\r
292 CallbackEntry->StatusCodeDataBuffer = (EFI_PHYSICAL_ADDRESS) (UINTN) NewBuffer;\r
cfc2ba61 293 CallbackEntry->BufferSize *= 2;\r
294 }\r
295 }\r
296 }\r
25126a05 297\r
cfc2ba61 298 //\r
299 // If data buffer is used up, do not report for this time.\r
300 //\r
301 if (CallbackEntry->EndPointer > (CallbackEntry->StatusCodeDataBuffer + CallbackEntry->BufferSize)) {\r
302 continue;\r
303 }\r
304\r
305 RscData->Type = Type;\r
306 RscData->Value = Value;\r
307 RscData->Instance = Instance;\r
308 if (CallerId != NULL) {\r
309 CopyGuid (&RscData->CallerId, CallerId);\r
310 }\r
311 if (Data != NULL) {\r
312 CopyMem (&RscData->Data, Data, Data->HeaderSize + Data->Size);\r
313 }\r
25126a05 314\r
cfc2ba61 315 Status = gBS->SignalEvent (CallbackEntry->Event);\r
25126a05 316 ASSERT_EFI_ERROR (Status);\r
317 }\r
318\r
319 //\r
320 // Restore the nest status of report\r
321 //\r
322 InterlockedCompareExchange32 (&mStatusCodeNestStatus, 1, 0);\r
323\r
324 return EFI_SUCCESS;\r
325}\r
326\r
327/**\r
328 Virtual address change notification call back. It converts global pointer\r
329 to virtual address.\r
330\r
331 @param Event Event whose notification function is being invoked.\r
332 @param Context Pointer to the notification function's context, which is\r
333 always zero in current implementation.\r
334\r
335**/\r
336VOID\r
337EFIAPI\r
338VirtualAddressChangeCallBack (\r
339 IN EFI_EVENT Event,\r
340 IN VOID *Context\r
341 )\r
342{\r
343 EFI_STATUS Status;\r
344 LIST_ENTRY *Link;\r
345 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;\r
346\r
347 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {\r
348 CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);\r
349 Status = EfiConvertFunctionPointer (0, (VOID **) &CallbackEntry->RscHandlerCallback);\r
350 ASSERT_EFI_ERROR (Status);\r
351 }\r
352\r
353 Status = EfiConvertList (\r
354 0,\r
355 &mCallbackListHead\r
356 );\r
357 ASSERT_EFI_ERROR (Status);\r
358}\r
359\r
360/**\r
361 Entry point of Generic Status Code Driver.\r
362\r
363 This function is the entry point of this Generic Status Code Driver.\r
364 It installs eport Stataus Code Handler Protocol and Status Code Runtime Protocol,\r
365 and registers event for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
366\r
367 @param ImageHandle The firmware allocated handle for the EFI image.\r
368 @param SystemTable A pointer to the EFI System Table.\r
369 \r
370 @retval EFI_SUCCESS The entry point is executed successfully.\r
371\r
372**/\r
373EFI_STATUS\r
374EFIAPI\r
375GenericStatusCodeRuntimeDxeEntry (\r
376 IN EFI_HANDLE ImageHandle,\r
377 IN EFI_SYSTEM_TABLE *SystemTable\r
378 )\r
379{\r
380 EFI_STATUS Status;\r
381\r
382 Status = gBS->InstallMultipleProtocolInterfaces (\r
383 &mHandle,\r
384 &gEfiRscHandlerProtocolGuid,\r
385 &mRscHandlerProtocol,\r
386 &gEfiStatusCodeRuntimeProtocolGuid,\r
387 &mStatusCodeProtocol,\r
388 NULL\r
389 );\r
390 ASSERT_EFI_ERROR (Status);\r
391\r
392 Status = gBS->CreateEventEx (\r
393 EVT_NOTIFY_SIGNAL,\r
394 TPL_NOTIFY,\r
395 VirtualAddressChangeCallBack,\r
396 NULL,\r
397 &gEfiEventVirtualAddressChangeGuid,\r
398 &mVirtualAddressChangeEvent\r
399 );\r
400 ASSERT_EFI_ERROR (Status);\r
401\r
402 return EFI_SUCCESS;\r
403}\r