2 SMM SwDispatch2 Protocol on SMM SwDispatch Protocol Thunk driver.
4 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available under
7 the terms and conditions of the BSD License that accompanies this distribution.
8 The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
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.
18 #include <FrameworkSmm.h>
20 #include <Protocol/SmmSwDispatch2.h>
21 #include <Protocol/SmmSwDispatch.h>
22 #include <Protocol/SmmControl.h>
23 #include <Protocol/SmmCpu.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Library/UefiDriverEntryPoint.h>
27 #include <Library/SmmServicesTableLib.h>
28 #include <Library/BaseLib.h>
29 #include <Library/IoLib.h>
30 #include <Library/DebugLib.h>
34 EFI_HANDLE DispatchHandle
;
35 UINTN SwSmiInputValue
;
36 UINTN DispatchFunction
;
37 } EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT
;
40 Register a child SMI source dispatch function for the specified software SMI.
42 This service registers a function (DispatchFunction) which will be called when the software
43 SMI source specified by RegisterContext->SwSmiCpuIndex is detected. On return,
44 DispatchHandle contains a unique handle which may be used later to unregister the function
47 @param[in] This Pointer to the EFI_SMM_SW_DISPATCH2_PROTOCOL instance.
48 @param[in] DispatchFunction Function to register for handler when the specified software
50 @param[in, out] RegisterContext Pointer to the dispatch function's context.
51 The caller fills this context in before calling
52 the register function to indicate to the register
53 function which Software SMI input value the
54 dispatch function should be invoked for.
55 @param[out] DispatchHandle Handle generated by the dispatcher to track the
58 @retval EFI_SUCCESS The dispatch function has been successfully
59 registered and the SMI source has been enabled.
60 @retval EFI_DEVICE_ERROR The SW driver was unable to enable the SMI source.
61 @retval EFI_INVALID_PARAMETER RegisterContext is invalid. The SW SMI input value
62 is not within valid range.
63 @retval EFI_OUT_OF_RESOURCES There is not enough memory (system or SMM) to manage this
65 @retval EFI_OUT_OF_RESOURCES A unique software SMI value could not be assigned
70 SmmSwDispatch2Register (
71 IN CONST EFI_SMM_SW_DISPATCH2_PROTOCOL
*This
,
72 IN EFI_SMM_HANDLER_ENTRY_POINT2 DispatchFunction
,
73 IN OUT EFI_SMM_SW_REGISTER_CONTEXT
*RegisterContext
,
74 OUT EFI_HANDLE
*DispatchHandle
78 Unregister a child SMI source dispatch function for the specified software SMI.
80 This service removes the handler associated with DispatchHandle so that it will no longer be
81 called in response to a software SMI.
83 @param[in] This Pointer to the EFI_SMM_SW_DISPATCH2_PROTOCOL instance.
84 @param[in] DispatchHandle Handle of dispatch function to deregister.
86 @retval EFI_SUCCESS The dispatch function has been successfully unregistered.
87 @retval EFI_INVALID_PARAMETER The DispatchHandle was not valid.
91 SmmSwDispatch2UnRegister (
92 IN CONST EFI_SMM_SW_DISPATCH2_PROTOCOL
*This
,
93 IN EFI_HANDLE DispatchHandle
96 EFI_SMM_SW_DISPATCH2_PROTOCOL gSmmSwDispatch2
= {
97 SmmSwDispatch2Register
,
98 SmmSwDispatch2UnRegister
,
102 EFI_SMM_SW_DISPATCH_PROTOCOL
*mSmmSwDispatch
;
103 UINT8 mSmiTriggerRegister
;
104 UINT8 mSmiDataRegister
;
106 EFI_SMM_CPU_PROTOCOL
*mSmmCpuProtocol
;
107 LIST_ENTRY mSmmSwDispatch2ThunkQueue
= INITIALIZE_LIST_HEAD_VARIABLE (mSmmSwDispatch2ThunkQueue
);
110 This function find SmmSwDispatch2Context by SwSmiInputValue.
112 @param SwSmiInputValue The SwSmiInputValue to indentify the SmmSwDispatch2 context
114 @return SmmSwDispatch2 context
116 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT
*
117 FindSmmSwDispatch2ContextBySwSmiInputValue (
118 IN UINTN SwSmiInputValue
122 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT
*ThunkContext
;
124 for (Link
= mSmmSwDispatch2ThunkQueue
.ForwardLink
;
125 Link
!= &mSmmSwDispatch2ThunkQueue
;
126 Link
= Link
->ForwardLink
) {
127 ThunkContext
= BASE_CR (
129 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT
,
132 if (ThunkContext
->SwSmiInputValue
== SwSmiInputValue
) {
140 This function find SmmSwDispatch2Context by DispatchHandle.
142 @param DispatchHandle The DispatchHandle to indentify the SmmSwDispatch2Thunk context
144 @return SmmSwDispatch2Thunk context
146 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT
*
147 FindSmmSwDispatch2ContextByDispatchHandle (
148 IN EFI_HANDLE DispatchHandle
152 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT
*ThunkContext
;
154 for (Link
= mSmmSwDispatch2ThunkQueue
.ForwardLink
;
155 Link
!= &mSmmSwDispatch2ThunkQueue
;
156 Link
= Link
->ForwardLink
) {
157 ThunkContext
= BASE_CR (
159 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT
,
162 if (ThunkContext
->DispatchHandle
== DispatchHandle
) {
170 Framework dispatch function for a Software SMI handler.
172 @param DispatchHandle The handle of this dispatch function.
173 @param DispatchContext The pointer to the dispatch function's context.
174 The SwSmiInputValue field is filled in
175 by the software dispatch driver prior to
176 invoking this dispatch function.
177 The dispatch function will only be called
178 for input values for which it is registered.
185 FrameworkDispatchFunction (
186 IN EFI_HANDLE DispatchHandle
,
187 IN EFI_SMM_SW_DISPATCH_CONTEXT
*DispatchContext
190 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT
*ThunkContext
;
191 EFI_SMM_HANDLER_ENTRY_POINT2 DispatchFunction
;
192 EFI_SMM_SW_REGISTER_CONTEXT RegisterContext
;
193 EFI_SMM_SW_CONTEXT SwContext
;
196 EFI_SMM_SAVE_STATE_IO_INFO IoInfo
;
202 ThunkContext
= FindSmmSwDispatch2ContextBySwSmiInputValue (DispatchContext
->SwSmiInputValue
);
203 ASSERT (ThunkContext
!= NULL
);
204 if (ThunkContext
== NULL
) {
209 // Construct new context
211 RegisterContext
.SwSmiInputValue
= DispatchContext
->SwSmiInputValue
;
212 Size
= sizeof(SwContext
);
213 SwContext
.CommandPort
= IoRead8 (mSmiTriggerRegister
);
214 SwContext
.DataPort
= IoRead8 (mSmiDataRegister
);
217 // Try to find which CPU trigger SWSMI
219 SwContext
.SwSmiCpuIndex
= 0;
220 for (Index
= 0; Index
< gSmst
->NumberOfCpus
; Index
++) {
221 Status
= mSmmCpuProtocol
->ReadSaveState (
224 EFI_SMM_SAVE_STATE_REGISTER_IO
,
228 if (EFI_ERROR (Status
)) {
231 if (IoInfo
.IoPort
== mSmiTriggerRegister
) {
235 SwContext
.SwSmiCpuIndex
= Index
;
243 DispatchFunction
= (EFI_SMM_HANDLER_ENTRY_POINT2
)ThunkContext
->DispatchFunction
;
254 Register a child SMI source dispatch function for the specified software SMI.
256 This service registers a function (DispatchFunction) which will be called when the software
257 SMI source specified by RegisterContext->SwSmiCpuIndex is detected. On return,
258 DispatchHandle contains a unique handle which may be used later to unregister the function
261 @param[in] This Pointer to the EFI_SMM_SW_DISPATCH2_PROTOCOL instance.
262 @param[in] DispatchFunction Function to register for handler when the specified software
264 @param[in, out] RegisterContext Pointer to the dispatch function's context.
265 The caller fills this context in before calling
266 the register function to indicate to the register
267 function which Software SMI input value the
268 dispatch function should be invoked for.
269 @param[out] DispatchHandle Handle generated by the dispatcher to track the
272 @retval EFI_SUCCESS The dispatch function has been successfully
273 registered and the SMI source has been enabled.
274 @retval EFI_DEVICE_ERROR The SW driver was unable to enable the SMI source.
275 @retval EFI_INVALID_PARAMETER RegisterContext is invalid. The SW SMI input value
276 is not within valid range.
277 @retval EFI_OUT_OF_RESOURCES There is not enough memory (system or SMM) to manage this
279 @retval EFI_OUT_OF_RESOURCES A unique software SMI value could not be assigned
284 SmmSwDispatch2Register (
285 IN CONST EFI_SMM_SW_DISPATCH2_PROTOCOL
*This
,
286 IN EFI_SMM_HANDLER_ENTRY_POINT2 DispatchFunction
,
287 IN OUT EFI_SMM_SW_REGISTER_CONTEXT
*RegisterContext
,
288 OUT EFI_HANDLE
*DispatchHandle
291 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT
*ThunkContext
;
292 EFI_SMM_SW_DISPATCH_CONTEXT DispatchContext
;
296 if (RegisterContext
->SwSmiInputValue
== (UINTN
)-1) {
298 // If SwSmiInputValue is set to (UINTN) -1 then a unique value will be assigned and returned in the structure.
300 Status
= EFI_NOT_FOUND
;
301 for (Index
= 1; Index
< gSmmSwDispatch2
.MaximumSwiValue
; Index
++) {
302 DispatchContext
.SwSmiInputValue
= Index
;
303 Status
= mSmmSwDispatch
->Register (
305 FrameworkDispatchFunction
,
309 if (!EFI_ERROR (Status
)) {
310 RegisterContext
->SwSmiInputValue
= Index
;
314 if (RegisterContext
->SwSmiInputValue
== (UINTN
)-1) {
315 return EFI_OUT_OF_RESOURCES
;
318 DispatchContext
.SwSmiInputValue
= RegisterContext
->SwSmiInputValue
;
319 Status
= mSmmSwDispatch
->Register (
321 FrameworkDispatchFunction
,
326 if (!EFI_ERROR (Status
)) {
330 Status
= gSmst
->SmmAllocatePool (
331 EfiRuntimeServicesData
,
332 sizeof(*ThunkContext
),
333 (VOID
**)&ThunkContext
335 ASSERT_EFI_ERROR (Status
);
336 if (EFI_ERROR (Status
)) {
337 mSmmSwDispatch
->UnRegister (mSmmSwDispatch
, *DispatchHandle
);
338 return EFI_OUT_OF_RESOURCES
;
341 ThunkContext
->SwSmiInputValue
= RegisterContext
->SwSmiInputValue
;
342 ThunkContext
->DispatchFunction
= (UINTN
)DispatchFunction
;
343 ThunkContext
->DispatchHandle
= *DispatchHandle
;
344 InsertTailList (&mSmmSwDispatch2ThunkQueue
, &ThunkContext
->Link
);
351 Unregister a child SMI source dispatch function for the specified software SMI.
353 This service removes the handler associated with DispatchHandle so that it will no longer be
354 called in response to a software SMI.
356 @param[in] This Pointer to the EFI_SMM_SW_DISPATCH2_PROTOCOL instance.
357 @param[in] DispatchHandle Handle of dispatch function to deregister.
359 @retval EFI_SUCCESS The dispatch function has been successfully unregistered.
360 @retval EFI_INVALID_PARAMETER The DispatchHandle was not valid.
364 SmmSwDispatch2UnRegister (
365 IN CONST EFI_SMM_SW_DISPATCH2_PROTOCOL
*This
,
366 IN EFI_HANDLE DispatchHandle
369 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT
*ThunkContext
;
372 Status
= mSmmSwDispatch
->UnRegister (mSmmSwDispatch
, DispatchHandle
);
373 if (!EFI_ERROR (Status
)) {
377 ThunkContext
= FindSmmSwDispatch2ContextByDispatchHandle (DispatchHandle
);
378 ASSERT (ThunkContext
!= NULL
);
379 if (ThunkContext
!= NULL
) {
380 RemoveEntryList (&ThunkContext
->Link
);
381 gSmst
->SmmFreePool (ThunkContext
);
389 Entry Point for this thunk driver.
391 @param[in] ImageHandle Image handle of this driver.
392 @param[in] SystemTable A Pointer to the EFI System Table.
394 @retval EFI_SUCCESS The entry point is executed successfully.
395 @retval other Some error occurred when executing this entry point.
399 SmmSwDispatch2ThunkMain (
400 IN EFI_HANDLE ImageHandle
,
401 IN EFI_SYSTEM_TABLE
*SystemTable
405 EFI_SMM_CONTROL_PROTOCOL
*SmmControl
;
406 EFI_SMM_CONTROL_REGISTER RegisterInfo
;
409 // Locate Framework SMM SwDispatch Protocol
411 Status
= gBS
->LocateProtocol (
412 &gEfiSmmSwDispatchProtocolGuid
,
414 (VOID
**)&mSmmSwDispatch
416 ASSERT_EFI_ERROR (Status
);
417 gSmmSwDispatch2
.MaximumSwiValue
= mSmmSwDispatch
->MaximumSwiValue
;
418 if (gSmmSwDispatch2
.MaximumSwiValue
== 0x0) {
419 DEBUG ((EFI_D_ERROR
, "BUGBUG: MaximumSwiValue is 0, work-around to make it 0xFF\n"));
420 gSmmSwDispatch2
.MaximumSwiValue
= 0xFF;
424 // Locate Framework SMM Control Protocol
426 Status
= gBS
->LocateProtocol (
427 &gEfiSmmControlProtocolGuid
,
432 ASSERT_EFI_ERROR (Status
);
433 Status
= SmmControl
->GetRegisterInfo (
437 ASSERT_EFI_ERROR (Status
);
438 mSmiTriggerRegister
= RegisterInfo
.SmiTriggerRegister
;
439 mSmiDataRegister
= RegisterInfo
.SmiDataRegister
;
442 // Locate PI SMM CPU protocol
444 Status
= gSmst
->SmmLocateProtocol (
445 &gEfiSmmCpuProtocolGuid
,
447 (VOID
**)&mSmmCpuProtocol
449 ASSERT_EFI_ERROR (Status
);
452 // Publish PI SMM SwDispatch2 Protocol
455 Status
= gSmst
->SmmInstallProtocolInterface (
457 &gEfiSmmSwDispatch2ProtocolGuid
,
458 EFI_NATIVE_INTERFACE
,
461 ASSERT_EFI_ERROR (Status
);