]> git.proxmox.com Git - mirror_edk2.git/blob - Vlv2TbltDevicePkg/SmmSwDispatch2OnSmmSwDispatchThunk/SmmSwDispatch2OnSmmSwDispatchThunk.c
SignedCapsulePkg: Replace [Ascii|Unicode]ValueToString
[mirror_edk2.git] / Vlv2TbltDevicePkg / SmmSwDispatch2OnSmmSwDispatchThunk / SmmSwDispatch2OnSmmSwDispatchThunk.c
1 /** @file
2 SMM SwDispatch2 Protocol on SMM SwDispatch Protocol Thunk driver.
3
4 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
5
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.
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
17 #include <PiDxe.h>
18 #include <FrameworkSmm.h>
19
20 #include <Protocol/SmmSwDispatch2.h>
21 #include <Protocol/SmmSwDispatch.h>
22 #include <Protocol/SmmControl.h>
23 #include <Protocol/SmmCpu.h>
24
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>
31
32 typedef struct {
33 LIST_ENTRY Link;
34 EFI_HANDLE DispatchHandle;
35 UINTN SwSmiInputValue;
36 UINTN DispatchFunction;
37 } EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT;
38
39 /**
40 Register a child SMI source dispatch function for the specified software SMI.
41
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
45 using UnRegister().
46
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
49 SMI is generated.
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
56 function instance.
57
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
64 child.
65 @retval EFI_OUT_OF_RESOURCES A unique software SMI value could not be assigned
66 for this dispatch.
67 **/
68 EFI_STATUS
69 EFIAPI
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
75 );
76
77 /**
78 Unregister a child SMI source dispatch function for the specified software SMI.
79
80 This service removes the handler associated with DispatchHandle so that it will no longer be
81 called in response to a software SMI.
82
83 @param[in] This Pointer to the EFI_SMM_SW_DISPATCH2_PROTOCOL instance.
84 @param[in] DispatchHandle Handle of dispatch function to deregister.
85
86 @retval EFI_SUCCESS The dispatch function has been successfully unregistered.
87 @retval EFI_INVALID_PARAMETER The DispatchHandle was not valid.
88 **/
89 EFI_STATUS
90 EFIAPI
91 SmmSwDispatch2UnRegister (
92 IN CONST EFI_SMM_SW_DISPATCH2_PROTOCOL *This,
93 IN EFI_HANDLE DispatchHandle
94 );
95
96 EFI_SMM_SW_DISPATCH2_PROTOCOL gSmmSwDispatch2 = {
97 SmmSwDispatch2Register,
98 SmmSwDispatch2UnRegister,
99 0 // MaximumSwiValue
100 };
101
102 EFI_SMM_SW_DISPATCH_PROTOCOL *mSmmSwDispatch;
103 UINT8 mSmiTriggerRegister;
104 UINT8 mSmiDataRegister;
105
106 EFI_SMM_CPU_PROTOCOL *mSmmCpuProtocol;
107 LIST_ENTRY mSmmSwDispatch2ThunkQueue = INITIALIZE_LIST_HEAD_VARIABLE (mSmmSwDispatch2ThunkQueue);
108
109 /**
110 This function find SmmSwDispatch2Context by SwSmiInputValue.
111
112 @param SwSmiInputValue The SwSmiInputValue to indentify the SmmSwDispatch2 context
113
114 @return SmmSwDispatch2 context
115 **/
116 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT *
117 FindSmmSwDispatch2ContextBySwSmiInputValue (
118 IN UINTN SwSmiInputValue
119 )
120 {
121 LIST_ENTRY *Link;
122 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT *ThunkContext;
123
124 for (Link = mSmmSwDispatch2ThunkQueue.ForwardLink;
125 Link != &mSmmSwDispatch2ThunkQueue;
126 Link = Link->ForwardLink) {
127 ThunkContext = BASE_CR (
128 Link,
129 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT,
130 Link
131 );
132 if (ThunkContext->SwSmiInputValue == SwSmiInputValue) {
133 return ThunkContext;
134 }
135 }
136 return NULL;
137 }
138
139 /**
140 This function find SmmSwDispatch2Context by DispatchHandle.
141
142 @param DispatchHandle The DispatchHandle to indentify the SmmSwDispatch2Thunk context
143
144 @return SmmSwDispatch2Thunk context
145 **/
146 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT *
147 FindSmmSwDispatch2ContextByDispatchHandle (
148 IN EFI_HANDLE DispatchHandle
149 )
150 {
151 LIST_ENTRY *Link;
152 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT *ThunkContext;
153
154 for (Link = mSmmSwDispatch2ThunkQueue.ForwardLink;
155 Link != &mSmmSwDispatch2ThunkQueue;
156 Link = Link->ForwardLink) {
157 ThunkContext = BASE_CR (
158 Link,
159 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT,
160 Link
161 );
162 if (ThunkContext->DispatchHandle == DispatchHandle) {
163 return ThunkContext;
164 }
165 }
166 return NULL;
167 }
168
169 /**
170 Framework dispatch function for a Software SMI handler.
171
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.
179
180 @return None
181
182 **/
183 VOID
184 EFIAPI
185 FrameworkDispatchFunction (
186 IN EFI_HANDLE DispatchHandle,
187 IN EFI_SMM_SW_DISPATCH_CONTEXT *DispatchContext
188 )
189 {
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;
194 UINTN Size;
195 UINTN Index;
196 EFI_SMM_SAVE_STATE_IO_INFO IoInfo;
197 EFI_STATUS Status;
198
199 //
200 // Search context
201 //
202 ThunkContext = FindSmmSwDispatch2ContextBySwSmiInputValue (DispatchContext->SwSmiInputValue);
203 ASSERT (ThunkContext != NULL);
204 if (ThunkContext == NULL) {
205 return ;
206 }
207
208 //
209 // Construct new context
210 //
211 RegisterContext.SwSmiInputValue = DispatchContext->SwSmiInputValue;
212 Size = sizeof(SwContext);
213 SwContext.CommandPort = IoRead8 (mSmiTriggerRegister);
214 SwContext.DataPort = IoRead8 (mSmiDataRegister);
215
216 //
217 // Try to find which CPU trigger SWSMI
218 //
219 SwContext.SwSmiCpuIndex = 0;
220 for (Index = 0; Index < gSmst->NumberOfCpus; Index++) {
221 Status = mSmmCpuProtocol->ReadSaveState (
222 mSmmCpuProtocol,
223 sizeof(IoInfo),
224 EFI_SMM_SAVE_STATE_REGISTER_IO,
225 Index,
226 &IoInfo
227 );
228 if (EFI_ERROR (Status)) {
229 continue;
230 }
231 if (IoInfo.IoPort == mSmiTriggerRegister) {
232 //
233 // Great! Find it.
234 //
235 SwContext.SwSmiCpuIndex = Index;
236 break;
237 }
238 }
239
240 //
241 // Dispatch
242 //
243 DispatchFunction = (EFI_SMM_HANDLER_ENTRY_POINT2)ThunkContext->DispatchFunction;
244 DispatchFunction (
245 DispatchHandle,
246 &RegisterContext,
247 &SwContext,
248 &Size
249 );
250 return ;
251 }
252
253 /**
254 Register a child SMI source dispatch function for the specified software SMI.
255
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
259 using UnRegister().
260
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
263 SMI is generated.
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
270 function instance.
271
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
278 child.
279 @retval EFI_OUT_OF_RESOURCES A unique software SMI value could not be assigned
280 for this dispatch.
281 **/
282 EFI_STATUS
283 EFIAPI
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
289 )
290 {
291 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT *ThunkContext;
292 EFI_SMM_SW_DISPATCH_CONTEXT DispatchContext;
293 EFI_STATUS Status;
294 UINTN Index;
295
296 if (RegisterContext->SwSmiInputValue == (UINTN)-1) {
297 //
298 // If SwSmiInputValue is set to (UINTN) -1 then a unique value will be assigned and returned in the structure.
299 //
300 Status = EFI_NOT_FOUND;
301 for (Index = 1; Index < gSmmSwDispatch2.MaximumSwiValue; Index++) {
302 DispatchContext.SwSmiInputValue = Index;
303 Status = mSmmSwDispatch->Register (
304 mSmmSwDispatch,
305 FrameworkDispatchFunction,
306 &DispatchContext,
307 DispatchHandle
308 );
309 if (!EFI_ERROR (Status)) {
310 RegisterContext->SwSmiInputValue = Index;
311 break;
312 }
313 }
314 if (RegisterContext->SwSmiInputValue == (UINTN)-1) {
315 return EFI_OUT_OF_RESOURCES;
316 }
317 } else {
318 DispatchContext.SwSmiInputValue = RegisterContext->SwSmiInputValue;
319 Status = mSmmSwDispatch->Register (
320 mSmmSwDispatch,
321 FrameworkDispatchFunction,
322 &DispatchContext,
323 DispatchHandle
324 );
325 }
326 if (!EFI_ERROR (Status)) {
327 //
328 // Register
329 //
330 Status = gSmst->SmmAllocatePool (
331 EfiRuntimeServicesData,
332 sizeof(*ThunkContext),
333 (VOID **)&ThunkContext
334 );
335 ASSERT_EFI_ERROR (Status);
336 if (EFI_ERROR (Status)) {
337 mSmmSwDispatch->UnRegister (mSmmSwDispatch, *DispatchHandle);
338 return EFI_OUT_OF_RESOURCES;
339 }
340
341 ThunkContext->SwSmiInputValue = RegisterContext->SwSmiInputValue;
342 ThunkContext->DispatchFunction = (UINTN)DispatchFunction;
343 ThunkContext->DispatchHandle = *DispatchHandle;
344 InsertTailList (&mSmmSwDispatch2ThunkQueue, &ThunkContext->Link);
345 }
346
347 return Status;
348 }
349
350 /**
351 Unregister a child SMI source dispatch function for the specified software SMI.
352
353 This service removes the handler associated with DispatchHandle so that it will no longer be
354 called in response to a software SMI.
355
356 @param[in] This Pointer to the EFI_SMM_SW_DISPATCH2_PROTOCOL instance.
357 @param[in] DispatchHandle Handle of dispatch function to deregister.
358
359 @retval EFI_SUCCESS The dispatch function has been successfully unregistered.
360 @retval EFI_INVALID_PARAMETER The DispatchHandle was not valid.
361 **/
362 EFI_STATUS
363 EFIAPI
364 SmmSwDispatch2UnRegister (
365 IN CONST EFI_SMM_SW_DISPATCH2_PROTOCOL *This,
366 IN EFI_HANDLE DispatchHandle
367 )
368 {
369 EFI_SMM_SW_DISPATCH2_THUNK_CONTEXT *ThunkContext;
370 EFI_STATUS Status;
371
372 Status = mSmmSwDispatch->UnRegister (mSmmSwDispatch, DispatchHandle);
373 if (!EFI_ERROR (Status)) {
374 //
375 // Unregister
376 //
377 ThunkContext = FindSmmSwDispatch2ContextByDispatchHandle (DispatchHandle);
378 ASSERT (ThunkContext != NULL);
379 if (ThunkContext != NULL) {
380 RemoveEntryList (&ThunkContext->Link);
381 gSmst->SmmFreePool (ThunkContext);
382 }
383 }
384
385 return Status;
386 }
387
388 /**
389 Entry Point for this thunk driver.
390
391 @param[in] ImageHandle Image handle of this driver.
392 @param[in] SystemTable A Pointer to the EFI System Table.
393
394 @retval EFI_SUCCESS The entry point is executed successfully.
395 @retval other Some error occurred when executing this entry point.
396 **/
397 EFI_STATUS
398 EFIAPI
399 SmmSwDispatch2ThunkMain (
400 IN EFI_HANDLE ImageHandle,
401 IN EFI_SYSTEM_TABLE *SystemTable
402 )
403 {
404 EFI_STATUS Status;
405 EFI_SMM_CONTROL_PROTOCOL *SmmControl;
406 EFI_SMM_CONTROL_REGISTER RegisterInfo;
407
408 //
409 // Locate Framework SMM SwDispatch Protocol
410 //
411 Status = gBS->LocateProtocol (
412 &gEfiSmmSwDispatchProtocolGuid,
413 NULL,
414 (VOID **)&mSmmSwDispatch
415 );
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;
421 }
422
423 //
424 // Locate Framework SMM Control Protocol
425 //
426 Status = gBS->LocateProtocol (
427 &gEfiSmmControlProtocolGuid,
428 NULL,
429 (VOID **)&SmmControl
430 );
431
432 ASSERT_EFI_ERROR (Status);
433 Status = SmmControl->GetRegisterInfo (
434 SmmControl,
435 &RegisterInfo
436 );
437 ASSERT_EFI_ERROR (Status);
438 mSmiTriggerRegister = RegisterInfo.SmiTriggerRegister;
439 mSmiDataRegister = RegisterInfo.SmiDataRegister;
440
441 //
442 // Locate PI SMM CPU protocol
443 //
444 Status = gSmst->SmmLocateProtocol (
445 &gEfiSmmCpuProtocolGuid,
446 NULL,
447 (VOID **)&mSmmCpuProtocol
448 );
449 ASSERT_EFI_ERROR (Status);
450
451 //
452 // Publish PI SMM SwDispatch2 Protocol
453 //
454 ImageHandle = NULL;
455 Status = gSmst->SmmInstallProtocolInterface (
456 &ImageHandle,
457 &gEfiSmmSwDispatch2ProtocolGuid,
458 EFI_NATIVE_INTERFACE,
459 &gSmmSwDispatch2
460 );
461 ASSERT_EFI_ERROR (Status);
462 return Status;
463 }
464