]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/ReportStatusCodeRouter/RuntimeDxe/ReportStatusCodeRouterRuntimeDxe.c
86809307238e8a15db3d056fd08354b80d056308
[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 - 2011, Intel Corporation. All rights reserved.<BR>
6 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
10
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.
13
14 **/
15
16 #include "ReportStatusCodeRouterRuntimeDxe.h"
17
18 EFI_HANDLE mHandle = NULL;
19 LIST_ENTRY mCallbackListHead = INITIALIZE_LIST_HEAD_VARIABLE (mCallbackListHead);
20 EFI_EVENT mVirtualAddressChangeEvent = NULL;
21
22 //
23 // Report operation nest status.
24 // If it is set, then the report operation has nested.
25 //
26 UINT32 mStatusCodeNestStatus = 0;
27
28 EFI_STATUS_CODE_PROTOCOL mStatusCodeProtocol = {
29 ReportDispatcher
30 };
31
32 EFI_RSC_HANDLER_PROTOCOL mRscHandlerProtocol = {
33 Register,
34 Unregister
35 };
36
37 /**
38 Event callback function to invoke status code handler in list.
39
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.
43
44 **/
45 VOID
46 EFIAPI
47 RscHandlerNotification (
48 IN EFI_EVENT Event,
49 IN VOID *Context
50 )
51 {
52 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
53 EFI_PHYSICAL_ADDRESS Address;
54 RSC_DATA_ENTRY *RscData;
55
56 CallbackEntry = (RSC_HANDLER_CALLBACK_ENTRY *) Context;
57
58 //
59 // Traverse the status code data buffer to parse all
60 // data to report.
61 //
62 Address = CallbackEntry->StatusCodeDataBuffer;
63 while (Address < CallbackEntry->EndPointer) {
64 RscData = (RSC_DATA_ENTRY *) (UINTN) Address;
65 CallbackEntry->RscHandlerCallback (
66 RscData->Type,
67 RscData->Value,
68 RscData->Instance,
69 &RscData->CallerId,
70 &RscData->Data
71 );
72
73 Address += (sizeof (RSC_DATA_ENTRY) + RscData->Data.Size);
74 Address = ALIGN_VARIABLE (Address);
75 }
76
77 CallbackEntry->EndPointer = CallbackEntry->StatusCodeDataBuffer;
78 }
79
80 /**
81 Register the callback function for ReportStatusCode() notification.
82
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.
96
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.
100
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
104 registered.
105 @retval EFI_ALREADY_STARTED The function was already registered. It can't be registered again.
106
107 **/
108 EFI_STATUS
109 EFIAPI
110 Register (
111 IN EFI_RSC_HANDLER_CALLBACK Callback,
112 IN EFI_TPL Tpl
113 )
114 {
115 EFI_STATUS Status;
116 LIST_ENTRY *Link;
117 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
118
119 if (Callback == NULL) {
120 return EFI_INVALID_PARAMETER;
121 }
122
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) {
126 //
127 // If the function was already registered. It can't be registered again.
128 //
129 return EFI_ALREADY_STARTED;
130 }
131 }
132
133 CallbackEntry = AllocateRuntimeZeroPool (sizeof (RSC_HANDLER_CALLBACK_ENTRY));
134 ASSERT (CallbackEntry != NULL);
135
136 CallbackEntry->Signature = RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE;
137 CallbackEntry->RscHandlerCallback = Callback;
138 CallbackEntry->Tpl = Tpl;
139
140 //
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.
147 //
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 (
153 EVT_NOTIFY_SIGNAL,
154 Tpl,
155 RscHandlerNotification,
156 CallbackEntry,
157 &CallbackEntry->Event
158 );
159 ASSERT_EFI_ERROR (Status);
160 }
161
162 InsertTailList (&mCallbackListHead, &CallbackEntry->Node);
163
164 return EFI_SUCCESS;
165 }
166
167 /**
168 Remove a previously registered callback function from the notification list.
169
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.
172
173 @param[in] Callback A pointer to a function of type EFI_RSC_HANDLER_CALLBACK that is to be
174 unregistered.
175
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.
179
180 **/
181 EFI_STATUS
182 EFIAPI
183 Unregister (
184 IN EFI_RSC_HANDLER_CALLBACK Callback
185 )
186 {
187 LIST_ENTRY *Link;
188 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
189
190 if (Callback == NULL) {
191 return EFI_INVALID_PARAMETER;
192 }
193
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) {
197 //
198 // If the function is found in list, delete it and return.
199 //
200 if (CallbackEntry->Tpl != TPL_HIGH_LEVEL) {
201 FreePool ((VOID *) (UINTN) CallbackEntry->StatusCodeDataBuffer);
202 gBS->CloseEvent (CallbackEntry->Event);
203 }
204 RemoveEntryList (&CallbackEntry->Node);
205 FreePool (CallbackEntry);
206 return EFI_SUCCESS;
207 }
208 }
209
210 return EFI_NOT_FOUND;
211 }
212
213 /**
214 Provides an interface that a software module can call to report a status code.
215
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
224 different callers.
225 @param Data This optional parameter may be used to pass additional data.
226
227 @retval EFI_SUCCESS The function completed successfully
228 @retval EFI_DEVICE_ERROR The function should not be completed due to a device error.
229
230 **/
231 EFI_STATUS
232 EFIAPI
233 ReportDispatcher (
234 IN EFI_STATUS_CODE_TYPE Type,
235 IN EFI_STATUS_CODE_VALUE Value,
236 IN UINT32 Instance,
237 IN EFI_GUID *CallerId OPTIONAL,
238 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
239 )
240 {
241 LIST_ENTRY *Link;
242 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
243 RSC_DATA_ENTRY *RscData;
244 EFI_STATUS Status;
245 VOID *NewBuffer;
246
247 //
248 // Use atom operation to avoid the reentant of report.
249 // If current status is not zero, then the function is reentrancy.
250 //
251 if (InterlockedCompareExchange32 (&mStatusCodeNestStatus, 0, 1) == 1) {
252 return EFI_DEVICE_ERROR;
253 }
254
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);
257
258 if ((CallbackEntry->Tpl == TPL_HIGH_LEVEL) || EfiAtRuntime ()) {
259 CallbackEntry->RscHandlerCallback (
260 Type,
261 Value,
262 Instance,
263 CallerId,
264 Data
265 );
266 continue;
267 }
268
269 //
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.
272 //
273 CallbackEntry->EndPointer = ALIGN_VARIABLE (CallbackEntry->EndPointer);
274 RscData = (RSC_DATA_ENTRY *) (UINTN) CallbackEntry->EndPointer;
275 CallbackEntry->EndPointer += sizeof (RSC_DATA_ENTRY);
276 if (Data != NULL) {
277 CallbackEntry->EndPointer += Data->Size;
278 }
279
280 //
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.
282 //
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
289 );
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;
294 }
295 }
296 }
297
298 //
299 // If data buffer is used up, do not report for this time.
300 //
301 if (CallbackEntry->EndPointer > (CallbackEntry->StatusCodeDataBuffer + CallbackEntry->BufferSize)) {
302 continue;
303 }
304
305 RscData->Type = Type;
306 RscData->Value = Value;
307 RscData->Instance = Instance;
308 if (CallerId != NULL) {
309 CopyGuid (&RscData->CallerId, CallerId);
310 }
311 if (Data != NULL) {
312 CopyMem (&RscData->Data, Data, Data->HeaderSize + Data->Size);
313 }
314
315 Status = gBS->SignalEvent (CallbackEntry->Event);
316 ASSERT_EFI_ERROR (Status);
317 }
318
319 //
320 // Restore the nest status of report
321 //
322 InterlockedCompareExchange32 (&mStatusCodeNestStatus, 1, 0);
323
324 return EFI_SUCCESS;
325 }
326
327 /**
328 Virtual address change notification call back. It converts global pointer
329 to virtual address.
330
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.
334
335 **/
336 VOID
337 EFIAPI
338 VirtualAddressChangeCallBack (
339 IN EFI_EVENT Event,
340 IN VOID *Context
341 )
342 {
343 EFI_STATUS Status;
344 LIST_ENTRY *Link;
345 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
346
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);
351 }
352
353 Status = EfiConvertList (
354 0,
355 &mCallbackListHead
356 );
357 ASSERT_EFI_ERROR (Status);
358 }
359
360 /**
361 Entry point of Generic Status Code Driver.
362
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.
366
367 @param ImageHandle The firmware allocated handle for the EFI image.
368 @param SystemTable A pointer to the EFI System Table.
369
370 @retval EFI_SUCCESS The entry point is executed successfully.
371
372 **/
373 EFI_STATUS
374 EFIAPI
375 GenericStatusCodeRuntimeDxeEntry (
376 IN EFI_HANDLE ImageHandle,
377 IN EFI_SYSTEM_TABLE *SystemTable
378 )
379 {
380 EFI_STATUS Status;
381
382 Status = gBS->InstallMultipleProtocolInterfaces (
383 &mHandle,
384 &gEfiRscHandlerProtocolGuid,
385 &mRscHandlerProtocol,
386 &gEfiStatusCodeRuntimeProtocolGuid,
387 &mStatusCodeProtocol,
388 NULL
389 );
390 ASSERT_EFI_ERROR (Status);
391
392 Status = gBS->CreateEventEx (
393 EVT_NOTIFY_SIGNAL,
394 TPL_NOTIFY,
395 VirtualAddressChangeCallBack,
396 NULL,
397 &gEfiEventVirtualAddressChangeGuid,
398 &mVirtualAddressChangeEvent
399 );
400 ASSERT_EFI_ERROR (Status);
401
402 return EFI_SUCCESS;
403 }