]> git.proxmox.com Git - mirror_edk2.git/blame - Vlv2TbltDevicePkg/SmmSwDispatch2OnSmmSwDispatchThunk/SmmSwDispatch2OnSmmSwDispatchThunk.c
SignedCapsulePkg: Replace [Ascii|Unicode]ValueToString
[mirror_edk2.git] / Vlv2TbltDevicePkg / SmmSwDispatch2OnSmmSwDispatchThunk / SmmSwDispatch2OnSmmSwDispatchThunk.c
CommitLineData
3cbfba02
DW
1/** @file\r
2 SMM SwDispatch2 Protocol on SMM SwDispatch Protocol Thunk driver.\r
3\r
4 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>\r
5 \r\r
6 This program and the accompanying materials are licensed and made available under\r\r
7 the terms and conditions of the BSD License that accompanies this distribution. \r\r
8 The full text of the license may be found at \r\r
9 http://opensource.org/licenses/bsd-license.php. \r\r
10 \r\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r\r
13 \r\r
14\r
15**/\r
16\r
17#include <PiDxe.h>\r
18#include <FrameworkSmm.h>\r
19\r
20#include <Protocol/SmmSwDispatch2.h>\r
21#include <Protocol/SmmSwDispatch.h>\r
22#include <Protocol/SmmControl.h>\r
23#include <Protocol/SmmCpu.h>\r
24\r
25#include <Library/UefiBootServicesTableLib.h>\r
26#include <Library/UefiDriverEntryPoint.h>\r
27#include <Library/SmmServicesTableLib.h>\r
28#include <Library/BaseLib.h>\r
29#include <Library/IoLib.h>\r
30#include <Library/DebugLib.h>\r
31\r
32typedef struct {\r
33 LIST_ENTRY Link;\r
34 EFI_HANDLE DispatchHandle;\r
35 UINTN SwSmiInputValue;\r
36 UINTN DispatchFunction;\r
37} EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT;\r
38\r
39/**\r
40 Register a child SMI source dispatch function for the specified software SMI.\r
41\r
42 This service registers a function (DispatchFunction) which will be called when the software\r
43 SMI source specified by RegisterContext->SwSmiCpuIndex is detected. On return,\r
44 DispatchHandle contains a unique handle which may be used later to unregister the function\r
45 using UnRegister().\r
46\r
47 @param[in] This Pointer to the EFI_SMM_SW_DISPATCH2_PROTOCOL instance.\r
48 @param[in] DispatchFunction Function to register for handler when the specified software\r
49 SMI is generated.\r
50 @param[in, out] RegisterContext Pointer to the dispatch function's context.\r
51 The caller fills this context in before calling\r
52 the register function to indicate to the register\r
53 function which Software SMI input value the\r
54 dispatch function should be invoked for.\r
55 @param[out] DispatchHandle Handle generated by the dispatcher to track the\r
56 function instance.\r
57\r
58 @retval EFI_SUCCESS The dispatch function has been successfully\r
59 registered and the SMI source has been enabled.\r
60 @retval EFI_DEVICE_ERROR The SW driver was unable to enable the SMI source.\r
61 @retval EFI_INVALID_PARAMETER RegisterContext is invalid. The SW SMI input value\r
62 is not within valid range.\r
63 @retval EFI_OUT_OF_RESOURCES There is not enough memory (system or SMM) to manage this\r
64 child.\r
65 @retval EFI_OUT_OF_RESOURCES A unique software SMI value could not be assigned\r
66 for this dispatch.\r
67**/\r
68EFI_STATUS\r
69EFIAPI\r
70SmmSwDispatch2Register (\r
71 IN CONST EFI_SMM_SW_DISPATCH2_PROTOCOL *This,\r
72 IN EFI_SMM_HANDLER_ENTRY_POINT2 DispatchFunction,\r
73 IN OUT EFI_SMM_SW_REGISTER_CONTEXT *RegisterContext,\r
74 OUT EFI_HANDLE *DispatchHandle\r
75 );\r
76\r
77/**\r
78 Unregister a child SMI source dispatch function for the specified software SMI.\r
79\r
80 This service removes the handler associated with DispatchHandle so that it will no longer be\r
81 called in response to a software SMI.\r
82\r
83 @param[in] This Pointer to the EFI_SMM_SW_DISPATCH2_PROTOCOL instance.\r
84 @param[in] DispatchHandle Handle of dispatch function to deregister.\r
85\r
86 @retval EFI_SUCCESS The dispatch function has been successfully unregistered.\r
87 @retval EFI_INVALID_PARAMETER The DispatchHandle was not valid.\r
88**/\r
89EFI_STATUS\r
90EFIAPI\r
91SmmSwDispatch2UnRegister (\r
92 IN CONST EFI_SMM_SW_DISPATCH2_PROTOCOL *This,\r
93 IN EFI_HANDLE DispatchHandle\r
94 );\r
95\r
96EFI_SMM_SW_DISPATCH2_PROTOCOL gSmmSwDispatch2 = {\r
97 SmmSwDispatch2Register,\r
98 SmmSwDispatch2UnRegister,\r
99 0 // MaximumSwiValue\r
100};\r
101\r
102EFI_SMM_SW_DISPATCH_PROTOCOL *mSmmSwDispatch;\r
103UINT8 mSmiTriggerRegister;\r
104UINT8 mSmiDataRegister;\r
105\r
106EFI_SMM_CPU_PROTOCOL *mSmmCpuProtocol;\r
107LIST_ENTRY mSmmSwDispatch2ThunkQueue = INITIALIZE_LIST_HEAD_VARIABLE (mSmmSwDispatch2ThunkQueue);\r
108\r
109/**\r
110 This function find SmmSwDispatch2Context by SwSmiInputValue.\r
111\r
112 @param SwSmiInputValue The SwSmiInputValue to indentify the SmmSwDispatch2 context\r
113\r
114 @return SmmSwDispatch2 context\r
115**/\r
116EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT *\r
117FindSmmSwDispatch2ContextBySwSmiInputValue (\r
118 IN UINTN SwSmiInputValue\r
119 )\r
120{\r
121 LIST_ENTRY *Link;\r
122 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT *ThunkContext;\r
123\r
124 for (Link = mSmmSwDispatch2ThunkQueue.ForwardLink;\r
125 Link != &mSmmSwDispatch2ThunkQueue;\r
126 Link = Link->ForwardLink) {\r
127 ThunkContext = BASE_CR (\r
128 Link,\r
129 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT,\r
130 Link\r
131 );\r
132 if (ThunkContext->SwSmiInputValue == SwSmiInputValue) {\r
133 return ThunkContext;\r
134 }\r
135 }\r
136 return NULL;\r
137}\r
138\r
139/**\r
140 This function find SmmSwDispatch2Context by DispatchHandle.\r
141\r
142 @param DispatchHandle The DispatchHandle to indentify the SmmSwDispatch2Thunk context\r
143\r
144 @return SmmSwDispatch2Thunk context\r
145**/\r
146EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT *\r
147FindSmmSwDispatch2ContextByDispatchHandle (\r
148 IN EFI_HANDLE DispatchHandle\r
149 )\r
150{\r
151 LIST_ENTRY *Link;\r
152 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT *ThunkContext;\r
153\r
154 for (Link = mSmmSwDispatch2ThunkQueue.ForwardLink;\r
155 Link != &mSmmSwDispatch2ThunkQueue;\r
156 Link = Link->ForwardLink) {\r
157 ThunkContext = BASE_CR (\r
158 Link,\r
159 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT,\r
160 Link\r
161 );\r
162 if (ThunkContext->DispatchHandle == DispatchHandle) {\r
163 return ThunkContext;\r
164 }\r
165 }\r
166 return NULL;\r
167}\r
168\r
169/**\r
170 Framework dispatch function for a Software SMI handler.\r
171\r
172 @param DispatchHandle The handle of this dispatch function.\r
173 @param DispatchContext The pointer to the dispatch function's context.\r
174 The SwSmiInputValue field is filled in\r
175 by the software dispatch driver prior to\r
176 invoking this dispatch function.\r
177 The dispatch function will only be called\r
178 for input values for which it is registered.\r
179\r
180 @return None\r
181\r
182**/\r
183VOID\r
184EFIAPI\r
185FrameworkDispatchFunction (\r
186 IN EFI_HANDLE DispatchHandle,\r
187 IN EFI_SMM_SW_DISPATCH_CONTEXT *DispatchContext\r
188 )\r
189{\r
190 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT *ThunkContext;\r
191 EFI_SMM_HANDLER_ENTRY_POINT2 DispatchFunction;\r
192 EFI_SMM_SW_REGISTER_CONTEXT RegisterContext;\r
193 EFI_SMM_SW_CONTEXT SwContext;\r
194 UINTN Size;\r
195 UINTN Index;\r
196 EFI_SMM_SAVE_STATE_IO_INFO IoInfo;\r
197 EFI_STATUS Status;\r
198\r
199 //\r
200 // Search context\r
201 //\r
202 ThunkContext = FindSmmSwDispatch2ContextBySwSmiInputValue (DispatchContext->SwSmiInputValue);\r
203 ASSERT (ThunkContext != NULL);\r
204 if (ThunkContext == NULL) {\r
205 return ;\r
206 }\r
207\r
208 //\r
209 // Construct new context\r
210 //\r
211 RegisterContext.SwSmiInputValue = DispatchContext->SwSmiInputValue;\r
212 Size = sizeof(SwContext);\r
213 SwContext.CommandPort = IoRead8 (mSmiTriggerRegister);\r
214 SwContext.DataPort = IoRead8 (mSmiDataRegister);\r
215\r
216 //\r
217 // Try to find which CPU trigger SWSMI\r
218 //\r
219 SwContext.SwSmiCpuIndex = 0;\r
220 for (Index = 0; Index < gSmst->NumberOfCpus; Index++) {\r
221 Status = mSmmCpuProtocol->ReadSaveState (\r
222 mSmmCpuProtocol,\r
223 sizeof(IoInfo),\r
224 EFI_SMM_SAVE_STATE_REGISTER_IO,\r
225 Index,\r
226 &IoInfo\r
227 );\r
228 if (EFI_ERROR (Status)) {\r
229 continue;\r
230 }\r
231 if (IoInfo.IoPort == mSmiTriggerRegister) {\r
232 //\r
233 // Great! Find it.\r
234 //\r
235 SwContext.SwSmiCpuIndex = Index;\r
236 break;\r
237 }\r
238 }\r
239\r
240 //\r
241 // Dispatch\r
242 //\r
243 DispatchFunction = (EFI_SMM_HANDLER_ENTRY_POINT2)ThunkContext->DispatchFunction;\r
244 DispatchFunction (\r
245 DispatchHandle,\r
246 &RegisterContext,\r
247 &SwContext,\r
248 &Size\r
249 );\r
250 return ;\r
251}\r
252\r
253/**\r
254 Register a child SMI source dispatch function for the specified software SMI.\r
255\r
256 This service registers a function (DispatchFunction) which will be called when the software\r
257 SMI source specified by RegisterContext->SwSmiCpuIndex is detected. On return,\r
258 DispatchHandle contains a unique handle which may be used later to unregister the function\r
259 using UnRegister().\r
260\r
261 @param[in] This Pointer to the EFI_SMM_SW_DISPATCH2_PROTOCOL instance.\r
262 @param[in] DispatchFunction Function to register for handler when the specified software\r
263 SMI is generated.\r
264 @param[in, out] RegisterContext Pointer to the dispatch function's context.\r
265 The caller fills this context in before calling\r
266 the register function to indicate to the register\r
267 function which Software SMI input value the\r
268 dispatch function should be invoked for.\r
269 @param[out] DispatchHandle Handle generated by the dispatcher to track the\r
270 function instance.\r
271\r
272 @retval EFI_SUCCESS The dispatch function has been successfully\r
273 registered and the SMI source has been enabled.\r
274 @retval EFI_DEVICE_ERROR The SW driver was unable to enable the SMI source.\r
275 @retval EFI_INVALID_PARAMETER RegisterContext is invalid. The SW SMI input value\r
276 is not within valid range.\r
277 @retval EFI_OUT_OF_RESOURCES There is not enough memory (system or SMM) to manage this\r
278 child.\r
279 @retval EFI_OUT_OF_RESOURCES A unique software SMI value could not be assigned\r
280 for this dispatch.\r
281**/\r
282EFI_STATUS\r
283EFIAPI\r
284SmmSwDispatch2Register (\r
285 IN CONST EFI_SMM_SW_DISPATCH2_PROTOCOL *This,\r
286 IN EFI_SMM_HANDLER_ENTRY_POINT2 DispatchFunction,\r
287 IN OUT EFI_SMM_SW_REGISTER_CONTEXT *RegisterContext,\r
288 OUT EFI_HANDLE *DispatchHandle\r
289 )\r
290{\r
291 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT *ThunkContext;\r
292 EFI_SMM_SW_DISPATCH_CONTEXT DispatchContext;\r
293 EFI_STATUS Status;\r
294 UINTN Index;\r
295\r
296 if (RegisterContext->SwSmiInputValue == (UINTN)-1) {\r
297 //\r
298 // If SwSmiInputValue is set to (UINTN) -1 then a unique value will be assigned and returned in the structure.\r
299 //\r
300 Status = EFI_NOT_FOUND;\r
301 for (Index = 1; Index < gSmmSwDispatch2.MaximumSwiValue; Index++) {\r
302 DispatchContext.SwSmiInputValue = Index;\r
303 Status = mSmmSwDispatch->Register (\r
304 mSmmSwDispatch,\r
305 FrameworkDispatchFunction,\r
306 &DispatchContext,\r
307 DispatchHandle\r
308 );\r
309 if (!EFI_ERROR (Status)) {\r
310 RegisterContext->SwSmiInputValue = Index;\r
311 break;\r
312 }\r
313 }\r
314 if (RegisterContext->SwSmiInputValue == (UINTN)-1) {\r
315 return EFI_OUT_OF_RESOURCES;\r
316 }\r
317 } else {\r
318 DispatchContext.SwSmiInputValue = RegisterContext->SwSmiInputValue;\r
319 Status = mSmmSwDispatch->Register (\r
320 mSmmSwDispatch,\r
321 FrameworkDispatchFunction,\r
322 &DispatchContext,\r
323 DispatchHandle\r
324 );\r
325 }\r
326 if (!EFI_ERROR (Status)) {\r
327 //\r
328 // Register\r
329 //\r
330 Status = gSmst->SmmAllocatePool (\r
331 EfiRuntimeServicesData,\r
332 sizeof(*ThunkContext),\r
333 (VOID **)&ThunkContext\r
334 );\r
335 ASSERT_EFI_ERROR (Status);\r
336 if (EFI_ERROR (Status)) {\r
337 mSmmSwDispatch->UnRegister (mSmmSwDispatch, *DispatchHandle);\r
338 return EFI_OUT_OF_RESOURCES;\r
339 }\r
340\r
341 ThunkContext->SwSmiInputValue = RegisterContext->SwSmiInputValue;\r
342 ThunkContext->DispatchFunction = (UINTN)DispatchFunction;\r
343 ThunkContext->DispatchHandle = *DispatchHandle;\r
344 InsertTailList (&mSmmSwDispatch2ThunkQueue, &ThunkContext->Link);\r
345 }\r
346\r
347 return Status;\r
348}\r
349\r
350/**\r
351 Unregister a child SMI source dispatch function for the specified software SMI.\r
352\r
353 This service removes the handler associated with DispatchHandle so that it will no longer be\r
354 called in response to a software SMI.\r
355\r
356 @param[in] This Pointer to the EFI_SMM_SW_DISPATCH2_PROTOCOL instance.\r
357 @param[in] DispatchHandle Handle of dispatch function to deregister.\r
358\r
359 @retval EFI_SUCCESS The dispatch function has been successfully unregistered.\r
360 @retval EFI_INVALID_PARAMETER The DispatchHandle was not valid.\r
361**/\r
362EFI_STATUS\r
363EFIAPI\r
364SmmSwDispatch2UnRegister (\r
365 IN CONST EFI_SMM_SW_DISPATCH2_PROTOCOL *This,\r
366 IN EFI_HANDLE DispatchHandle\r
367 )\r
368{\r
369 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT *ThunkContext;\r
370 EFI_STATUS Status;\r
371\r
372 Status = mSmmSwDispatch->UnRegister (mSmmSwDispatch, DispatchHandle);\r
373 if (!EFI_ERROR (Status)) {\r
374 //\r
375 // Unregister\r
376 //\r
377 ThunkContext = FindSmmSwDispatch2ContextByDispatchHandle (DispatchHandle);\r
378 ASSERT (ThunkContext != NULL);\r
379 if (ThunkContext != NULL) {\r
380 RemoveEntryList (&ThunkContext->Link);\r
381 gSmst->SmmFreePool (ThunkContext);\r
382 }\r
383 }\r
384\r
385 return Status;\r
386}\r
387\r
388/**\r
389 Entry Point for this thunk driver.\r
390\r
391 @param[in] ImageHandle Image handle of this driver.\r
392 @param[in] SystemTable A Pointer to the EFI System Table.\r
393\r
394 @retval EFI_SUCCESS The entry point is executed successfully.\r
395 @retval other Some error occurred when executing this entry point.\r
396**/\r
397EFI_STATUS\r
398EFIAPI\r
399SmmSwDispatch2ThunkMain (\r
400 IN EFI_HANDLE ImageHandle,\r
401 IN EFI_SYSTEM_TABLE *SystemTable\r
402 )\r
403{\r
404 EFI_STATUS Status;\r
405 EFI_SMM_CONTROL_PROTOCOL *SmmControl;\r
406 EFI_SMM_CONTROL_REGISTER RegisterInfo;\r
407\r
408 //\r
409 // Locate Framework SMM SwDispatch Protocol\r
410 //\r
411 Status = gBS->LocateProtocol (\r
412 &gEfiSmmSwDispatchProtocolGuid,\r
413 NULL,\r
414 (VOID **)&mSmmSwDispatch\r
415 );\r
416 ASSERT_EFI_ERROR (Status);\r
417 gSmmSwDispatch2.MaximumSwiValue = mSmmSwDispatch->MaximumSwiValue;\r
418 if (gSmmSwDispatch2.MaximumSwiValue == 0x0) {\r
419 DEBUG ((EFI_D_ERROR, "BUGBUG: MaximumSwiValue is 0, work-around to make it 0xFF\n"));\r
420 gSmmSwDispatch2.MaximumSwiValue = 0xFF;\r
421 }\r
422\r
423 //\r
424 // Locate Framework SMM Control Protocol\r
425 //\r
426 Status = gBS->LocateProtocol (\r
427 &gEfiSmmControlProtocolGuid,\r
428 NULL,\r
429 (VOID **)&SmmControl\r
430 );\r
431\r
432 ASSERT_EFI_ERROR (Status);\r
433 Status = SmmControl->GetRegisterInfo (\r
434 SmmControl,\r
435 &RegisterInfo\r
436 );\r
437 ASSERT_EFI_ERROR (Status);\r
438 mSmiTriggerRegister = RegisterInfo.SmiTriggerRegister;\r
439 mSmiDataRegister = RegisterInfo.SmiDataRegister;\r
440\r
441 //\r
442 // Locate PI SMM CPU protocol\r
443 //\r
444 Status = gSmst->SmmLocateProtocol (\r
445 &gEfiSmmCpuProtocolGuid,\r
446 NULL,\r
447 (VOID **)&mSmmCpuProtocol\r
448 );\r
449 ASSERT_EFI_ERROR (Status);\r
450\r
451 //\r
452 // Publish PI SMM SwDispatch2 Protocol\r
453 //\r
454 ImageHandle = NULL;\r
455 Status = gSmst->SmmInstallProtocolInterface (\r
456 &ImageHandle,\r
457 &gEfiSmmSwDispatch2ProtocolGuid,\r
458 EFI_NATIVE_INTERFACE,\r
459 &gSmmSwDispatch2\r
460 );\r
461 ASSERT_EFI_ERROR (Status);\r
462 return Status;\r
463}\r
464\r