]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/ReportStatusCodeRouter/RuntimeDxe/ReportStatusCodeRouterRuntimeDxe.c
Enable Report Status Code Router introduced in PI 1.2 for PEI and DXE.
[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, Intel Corporation
6 All rights reserved. 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 Register the callback function for ReportStatusCode() notification.
39
40 When this function is called the function pointer is added to an internal list and any future calls to
41 ReportStatusCode() will be forwarded to the Callback function. During the bootservices,
42 this is the callback for which this service can be invoked. The report status code router
43 will create an event such that the callback function is only invoked at the TPL for which it was
44 registered. The entity that registers for the callback should also register for an event upon
45 generation of exit boot services and invoke the unregister service.
46 If the handler does not have a TPL dependency, it should register for a callback at TPL high. The
47 router infrastructure will support making callbacks at runtime, but the caller for runtime invocation
48 must meet the following criteria:
49 1. must be a runtime driver type so that its memory is not reclaimed
50 2. not unregister at exit boot services so that the router will still have its callback address
51 3. the caller must be self-contained (eg. Not call out into any boot-service interfaces) and be
52 runtime safe, in general.
53
54 @param[in] Callback A pointer to a function of type EFI_RSC_HANDLER_CALLBACK that is called when
55 a call to ReportStatusCode() occurs.
56 @param[in] Tpl TPL at which callback can be safely invoked.
57
58 @retval EFI_SUCCESS Function was successfully registered.
59 @retval EFI_INVALID_PARAMETER The callback function was NULL.
60 @retval EFI_OUT_OF_RESOURCES The internal buffer ran out of space. No more functions can be
61 registered.
62 @retval EFI_ALREADY_STARTED The function was already registered. It can't be registered again.
63
64 **/
65 EFI_STATUS
66 EFIAPI
67 Register (
68 IN EFI_RSC_HANDLER_CALLBACK Callback,
69 IN EFI_TPL Tpl
70 )
71 {
72 LIST_ENTRY *Link;
73 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
74
75 if (Callback == NULL) {
76 return EFI_INVALID_PARAMETER;
77 }
78
79 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
80 CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
81 if (CallbackEntry->RscHandlerCallback == Callback) {
82 //
83 // If the function was already registered. It can't be registered again.
84 //
85 return EFI_ALREADY_STARTED;
86 }
87 }
88
89 CallbackEntry = AllocatePool (sizeof (RSC_HANDLER_CALLBACK_ENTRY));
90 ASSERT (CallbackEntry != NULL);
91
92 CallbackEntry->Signature = RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE;
93 CallbackEntry->RscHandlerCallback = Callback;
94 CallbackEntry->Tpl = Tpl;
95
96 InsertTailList (&mCallbackListHead, &CallbackEntry->Node);
97
98 return EFI_SUCCESS;
99 }
100
101 /**
102 Remove a previously registered callback function from the notification list.
103
104 A callback function must be unregistered before it is deallocated. It is important that any registered
105 callbacks that are not runtime complaint be unregistered when ExitBootServices() is called.
106
107 @param[in] Callback A pointer to a function of type EFI_RSC_HANDLER_CALLBACK that is to be
108 unregistered.
109
110 @retval EFI_SUCCESS The function was successfully unregistered.
111 @retval EFI_INVALID_PARAMETER The callback function was NULL.
112 @retval EFI_NOT_FOUND The callback function was not found to be unregistered.
113
114 **/
115 EFI_STATUS
116 EFIAPI
117 Unregister (
118 IN EFI_RSC_HANDLER_CALLBACK Callback
119 )
120 {
121 LIST_ENTRY *Link;
122 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
123
124 if (Callback == NULL) {
125 return EFI_INVALID_PARAMETER;
126 }
127
128 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
129 CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
130 if (CallbackEntry->RscHandlerCallback == Callback) {
131 //
132 // If the function is found in list, delete it and return.
133 //
134 RemoveEntryList (&CallbackEntry->Node);
135 FreePool (CallbackEntry);
136 return EFI_SUCCESS;
137 }
138 }
139
140 return EFI_NOT_FOUND;
141 }
142
143 /**
144 Event callback function to invoke status code handler in list.
145
146 @param Event Event whose notification function is being invoked.
147 @param Context Pointer to the notification function's context, which is
148 always zero in current implementation.
149
150 **/
151 VOID
152 EFIAPI
153 RscHandlerNotification (
154 IN EFI_EVENT Event,
155 IN VOID *Context
156 )
157 {
158 RSC_EVENT_CONTEXT *RscContext;
159
160 RscContext = (RSC_EVENT_CONTEXT *) Context;
161
162 RscContext->RscHandlerCallback (
163 RscContext->Type,
164 RscContext->Value,
165 RscContext->Instance,
166 RscContext->CallerId,
167 RscContext->Data
168 );
169 }
170
171 /**
172 Provides an interface that a software module can call to report a status code.
173
174 @param Type Indicates the type of status code being reported.
175 @param Value Describes the current status of a hardware or software entity.
176 This included information about the class and subclass that is used to
177 classify the entity as well as an operation.
178 @param Instance The enumeration of a hardware or software entity within
179 the system. Valid instance numbers start with 1.
180 @param CallerId This optional parameter may be used to identify the caller.
181 This parameter allows the status code driver to apply different rules to
182 different callers.
183 @param Data This optional parameter may be used to pass additional data.
184
185 @retval EFI_SUCCESS The function completed successfully
186 @retval EFI_DEVICE_ERROR The function should not be completed due to a device error.
187
188 **/
189 EFI_STATUS
190 EFIAPI
191 ReportDispatcher (
192 IN EFI_STATUS_CODE_TYPE Type,
193 IN EFI_STATUS_CODE_VALUE Value,
194 IN UINT32 Instance,
195 IN EFI_GUID *CallerId OPTIONAL,
196 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
197 )
198 {
199 LIST_ENTRY *Link;
200 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
201 RSC_EVENT_CONTEXT Context;
202 EFI_EVENT Event;
203 EFI_STATUS Status;
204
205 //
206 // Use atom operation to avoid the reentant of report.
207 // If current status is not zero, then the function is reentrancy.
208 //
209 if (InterlockedCompareExchange32 (&mStatusCodeNestStatus, 0, 1) == 1) {
210 return EFI_DEVICE_ERROR;
211 }
212
213 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
214 CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
215
216 if ((CallbackEntry->Tpl == TPL_HIGH_LEVEL) || EfiAtRuntime ()) {
217 CallbackEntry->RscHandlerCallback (
218 Type,
219 Value,
220 Instance,
221 CallerId,
222 Data
223 );
224 continue;
225 }
226
227 Context.RscHandlerCallback = CallbackEntry->RscHandlerCallback;
228 Context.Type = Type;
229 Context.Value = Value;
230 Context.Instance = Instance;
231 Context.CallerId = CallerId;
232 Context.Data = Data;
233
234 Status = gBS->CreateEvent (
235 EVT_NOTIFY_SIGNAL,
236 CallbackEntry->Tpl,
237 RscHandlerNotification,
238 &Context,
239 &Event
240 );
241 ASSERT_EFI_ERROR (Status);
242
243 Status = gBS->SignalEvent (Event);
244 ASSERT_EFI_ERROR (Status);
245
246 Status = gBS->CloseEvent (Event);
247 ASSERT_EFI_ERROR (Status);
248 }
249
250 //
251 // Restore the nest status of report
252 //
253 InterlockedCompareExchange32 (&mStatusCodeNestStatus, 1, 0);
254
255 return EFI_SUCCESS;
256 }
257
258 /**
259 Virtual address change notification call back. It converts global pointer
260 to virtual address.
261
262 @param Event Event whose notification function is being invoked.
263 @param Context Pointer to the notification function's context, which is
264 always zero in current implementation.
265
266 **/
267 VOID
268 EFIAPI
269 VirtualAddressChangeCallBack (
270 IN EFI_EVENT Event,
271 IN VOID *Context
272 )
273 {
274 EFI_STATUS Status;
275 LIST_ENTRY *Link;
276 RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
277
278 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
279 CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
280 Status = EfiConvertFunctionPointer (0, (VOID **) &CallbackEntry->RscHandlerCallback);
281 ASSERT_EFI_ERROR (Status);
282 }
283
284 Status = EfiConvertList (
285 0,
286 &mCallbackListHead
287 );
288 ASSERT_EFI_ERROR (Status);
289 }
290
291 /**
292 Entry point of Generic Status Code Driver.
293
294 This function is the entry point of this Generic Status Code Driver.
295 It installs eport Stataus Code Handler Protocol and Status Code Runtime Protocol,
296 and registers event for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
297
298 @param ImageHandle The firmware allocated handle for the EFI image.
299 @param SystemTable A pointer to the EFI System Table.
300
301 @retval EFI_SUCCESS The entry point is executed successfully.
302
303 **/
304 EFI_STATUS
305 EFIAPI
306 GenericStatusCodeRuntimeDxeEntry (
307 IN EFI_HANDLE ImageHandle,
308 IN EFI_SYSTEM_TABLE *SystemTable
309 )
310 {
311 EFI_STATUS Status;
312
313 Status = gBS->InstallMultipleProtocolInterfaces (
314 &mHandle,
315 &gEfiRscHandlerProtocolGuid,
316 &mRscHandlerProtocol,
317 &gEfiStatusCodeRuntimeProtocolGuid,
318 &mStatusCodeProtocol,
319 NULL
320 );
321 ASSERT_EFI_ERROR (Status);
322
323 Status = gBS->CreateEventEx (
324 EVT_NOTIFY_SIGNAL,
325 TPL_NOTIFY,
326 VirtualAddressChangeCallBack,
327 NULL,
328 &gEfiEventVirtualAddressChangeGuid,
329 &mVirtualAddressChangeEvent
330 );
331 ASSERT_EFI_ERROR (Status);
332
333 return EFI_SUCCESS;
334 }