2 SMM Periodic SMI Library.
4 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Protocol/SmmPeriodicTimerDispatch2.h>
19 #include <Library/BaseLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/SynchronizationLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/TimerLib.h>
24 #include <Library/MemoryAllocationLib.h>
25 #include <Library/SmmServicesTableLib.h>
27 #include <Library/SmmPeriodicSmiLib.h>
30 /// Define the number of periodic SMI handler entries that should be allocated in
31 /// the constructor for gPeriodicSmiLibraryHandlers and also use this value as the
32 /// number of entries to add to gPeriodicSmiLibraryHandlers when gPeriodicSmiLibraryHandlers
35 #define PERIODIC_SMI_LIBRARY_ALLOCATE_SIZE 0x08
38 /// Signature for a PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure
40 #define PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE SIGNATURE_32 ('P', 'S', 'M', 'I')
43 /// Structure that contains state information for an enabled periodic SMI handler
47 /// Signature value that must be set to PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE
51 /// The dispatch function to called to invoke an enabled periodic SMI handler.
53 PERIODIC_SMI_LIBRARY_HANDLER DispatchFunction
;
55 /// The context to pass into DispatchFunction
59 /// The tick period in 100 ns units that DispatchFunction should be called.
63 /// The Cpu number that is required to execute DispatchFunction. If Cpu is
64 /// set to PERIODIC_SMI_LIBRARY_ANY_CPU, then DispatchFunction may be executed
69 /// The size, in bytes, of the stack allocated for a periodic SMI handler.
70 /// This value must be a multiple of EFI_PAGE_SIZE.
74 /// A pointer to the stack allocated using AllocatePages(). This field will
75 /// be NULL if StackSize is 0.
79 /// Spin lock used to wait for an AP to complete the execution of a periodic SMI handler
81 SPIN_LOCK DispatchLock
;
83 /// The rate in Hz of the performance counter that is used to measure the
84 /// amount of time that a periodic SMI handler executes.
86 UINT64 PerfomanceCounterRate
;
88 /// The start count value of the performance counter that is used to measure
89 /// the amount of time that a periodic SMI handler executes.
91 UINT64 PerfomanceCounterStartValue
;
93 /// The end count value of the performance counter that is used to measure
94 /// the amount of time that a periodic SMI handler executes.
96 UINT64 PerfomanceCounterEndValue
;
98 /// The context record passed into the Register() function of the SMM Periodic
99 /// Timer Dispatch Protocol when a periodic SMI handler is enabled.
101 EFI_SMM_PERIODIC_TIMER_REGISTER_CONTEXT RegisterContext
;
103 /// The handle returned from the Register() function of the SMM Periodic
104 /// Timer Dispatch Protocol when a periodic SMI handler is enabled.
106 EFI_HANDLE DispatchHandle
;
108 /// The total number of performance counter ticks that the periodic SMI handler
109 /// has been executing in its current invocation.
111 UINT64 DispatchTotalTime
;
113 /// The performance counter value that was captured the last time that the
114 /// periodic SMI handler called PeriodcSmiExecutionTime(). This allows the
115 /// time value returned by PeriodcSmiExecutionTime() to be accurate even when
116 /// the performance counter rolls over.
118 UINT64 DispatchCheckPointTime
;
120 /// Buffer used to save the context when control is transfer from this library
121 /// to an enabled periodic SMI handler. This saved context is used when the
122 /// periodic SMI handler exits or yields.
124 BASE_LIBRARY_JUMP_BUFFER DispatchJumpBuffer
;
126 /// Flag that is set to TRUE when a periodic SMI handler requests to yield
127 /// using PeriodicSmiYield(). When this flag IS TRUE, YieldJumpBuffer is
128 /// valid. When this flag is FALSE, YieldJumpBuffer is not valid.
132 /// Buffer used to save the context when a periodic SMI handler requests to
133 /// yield using PeriodicSmiYield(). This context is used to resume the
134 /// execution of a periodic SMI handler the next time control is transferd
135 /// to the periodic SMI handler that yielded.
137 BASE_LIBRARY_JUMP_BUFFER YieldJumpBuffer
;
139 /// The amount of time, in 100 ns units, that have elapsed since the last
140 /// time the periodic SMI handler was invoked.
143 } PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
;
146 Macro that returns a pointer to a PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
147 structure based on a pointer to a RegisterContext field.
150 #define PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_REGISTER_CONTEXT(a) \
153 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT, \
155 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE \
159 /// Pointer to the SMM Periodic Timer Disatch Protocol that was located in the constuctor.
161 EFI_SMM_PERIODIC_TIMER_DISPATCH2_PROTOCOL
*gSmmPeriodicTimerDispatch2
= NULL
;
164 /// Pointer to a table of supported periodic SMI tick periods in 100 ns units
165 /// sorted from largest to smallest terminated by a tick period value of 0.
166 /// This table is allocated using AllocatePool() in the constructor and filled
167 /// in based on the values returned from the SMM Periodic Timer Dispatch 2 Protocol
168 /// function GetNextShorterInterval().
170 UINT64
*gSmiTickPeriodTable
= NULL
;
173 /// The number entries in gPeriodicSmiLibraryHandlers
175 UINTN gNumberOfPeriodicSmiLibraryHandlers
= 0;
178 /// Table of periodic SMI handlers that this library is currently managing. This
179 /// table is allocated using AllocatePool()
181 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*gPeriodicSmiLibraryHandlers
= NULL
;
184 /// The index of gPeriodicSmiLibraryHandlers that is currently being executed.
185 /// Is set to -1 if no periodic SMI handler is currently being executed.
187 INTN gActivePeriodicSmiLibraryHandlerIndex
= -1;
190 Internal worker function that returns a pointer to the
191 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure associated with the periodic
192 SMI handler that is currently being executed. If a periodic SMI handler is
193 not currently being executed, the NULL is returned.
195 @retval NULL A periodic SMI handler is not currently being executed.
196 @retval other Pointer to the PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
197 associated with the active periodic SMI handler.
200 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*
201 GetActivePeriodicSmiLibraryHandler (
205 if (gActivePeriodicSmiLibraryHandlerIndex
< 0) {
207 // Return NULL if index is negative, which means that there is no active
208 // periodic SMI handler.
214 // Return a pointer to the active periodic SMI handler context
216 return &gPeriodicSmiLibraryHandlers
[gActivePeriodicSmiLibraryHandlerIndex
];
220 Internal worker function that returns a pointer to the
221 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure associated with the
222 DispatchHandle that was returned when the periodic SMI handler was enabled
223 with PeriodicSmiEnable(). If DispatchHandle is NULL, then the active
224 periodic SMI handler is returned. If DispatchHandle is NULL and there is
225 no active periodic SMI handler, then NULL is returned.
227 @param[in] DispatchHandle DispatchHandle that was returned when the periodic
228 SMI handler was enabled with PeriodicSmiEnable().
229 This is an optional parameter that may be NULL.
230 If this parameter is NULL, then the active periodic
231 SMI handler is returned.
233 @retval NULL DispatchHandle is NULL and there is no active periodic SMI
235 @retval NULL DispatchHandle does not match any of the periodic SMI handlers
236 that have been enabled with PeriodicSmiEnable().
237 @retval other Pointer to the PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
238 associated with the DispatchHandle.
241 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*
242 LookupPeriodicSmiLibraryHandler (
243 IN EFI_HANDLE DispatchHandle OPTIONAL
249 // If DispatchHandle is NULL, then return the active periodic SMI handler
251 if (DispatchHandle
== NULL
) {
252 return GetActivePeriodicSmiLibraryHandler ();
256 // Search the periodic SMI handler entries for a a matching DispatchHandle
258 for (Index
= 0; Index
< gNumberOfPeriodicSmiLibraryHandlers
; Index
++) {
259 if (gPeriodicSmiLibraryHandlers
[Index
].DispatchHandle
== DispatchHandle
) {
260 return &gPeriodicSmiLibraryHandlers
[Index
];
265 // No entries match DispatchHandle, so return NULL
271 Internal worker function that sets that active periodic SMI handler based on
272 the Context used when the periodic SMI handler was registered with the
273 SMM Periodic Timer Dispatch 2 Protocol. If Context is NULL, then the
274 state is updated to show that there is not active periodic SMI handler.
275 A pointer to the active PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure
278 @retval NULL Context is NULL.
279 @retval other Pointer to the PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
280 associated with Context.
283 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*
284 SetActivePeriodicSmiLibraryHandler (
285 IN CONST VOID
*Context OPTIONAL
288 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
290 if (Context
== NULL
) {
291 gActivePeriodicSmiLibraryHandlerIndex
= -1;
294 PeriodicSmiLibraryHandler
= PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_REGISTER_CONTEXT (Context
);
295 gActivePeriodicSmiLibraryHandlerIndex
= PeriodicSmiLibraryHandler
- gPeriodicSmiLibraryHandlers
;
296 return PeriodicSmiLibraryHandler
;
300 Internal worker function that returns a free entry for a new periodic
301 SMI handler. If no free entries are available, then additional
302 entries are allocated.
304 @retval NULL There are not enough resources available to to allocate a free entry.
305 @retval other Pointer to a free PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure.
308 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*
309 FindFreePeriodicSmiLibraryHandler (
314 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
317 // Search for a free entry in gPeriodicSmiLibraryHandlers
318 // A free entry must have a NULL DispatchHandle and a NULL Stack.
320 for (Index
= 0; Index
< gNumberOfPeriodicSmiLibraryHandlers
; Index
++) {
321 if (gPeriodicSmiLibraryHandlers
[Index
].DispatchHandle
!= NULL
) {
324 if (gPeriodicSmiLibraryHandlers
[Index
].Stack
!= NULL
) {
327 return &gPeriodicSmiLibraryHandlers
[Index
];
331 // If no free entries were found, then grow the table of periodic SMI handler entries
333 if (Index
== gNumberOfPeriodicSmiLibraryHandlers
) {
334 PeriodicSmiLibraryHandler
= ReallocatePool (
335 gNumberOfPeriodicSmiLibraryHandlers
* sizeof (PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
),
336 (gNumberOfPeriodicSmiLibraryHandlers
+ PERIODIC_SMI_LIBRARY_ALLOCATE_SIZE
) * sizeof (PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
),
337 gPeriodicSmiLibraryHandlers
339 if (PeriodicSmiLibraryHandler
== NULL
) {
342 gPeriodicSmiLibraryHandlers
= PeriodicSmiLibraryHandler
;
343 gNumberOfPeriodicSmiLibraryHandlers
+= PERIODIC_SMI_LIBRARY_ALLOCATE_SIZE
;
346 return &gPeriodicSmiLibraryHandlers
[Index
];
350 This function returns a pointer to a table of supported periodic
351 SMI tick periods in 100 ns units sorted from largest to smallest.
352 The table contains a array of UINT64 values terminated by a tick
353 period value of 0. The returned table must be treated as read-only
354 data and must not be freed.
356 @return A pointer to a table of UINT64 tick period values in
357 100ns units sorted from largest to smallest terminated
358 by a tick period of 0.
363 PeriodicSmiSupportedTickPeriod (
368 // Return the table allocated and populated by SmmPeriodicSmiLibConstructor()
370 return gSmiTickPeriodTable
;
374 This function returns the time in 100ns units since the periodic SMI
375 handler function was called. If the periodic SMI handler was resumed
376 through PeriodicSmiYield(), then the time returned is the time in
377 100ns units since PeriodicSmiYield() returned.
379 @return The actual time in 100ns units that the periodic SMI handler
380 has been executing. If this function is not called from within
381 an enabled periodic SMI handler, then 0 is returned.
386 PeriodicSmiExecutionTime (
390 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
395 // If there is no active periodic SMI handler, then return 0
397 PeriodicSmiLibraryHandler
= GetActivePeriodicSmiLibraryHandler ();
398 if (PeriodicSmiLibraryHandler
== NULL
) {
403 // Get the current performance counter value
405 Current
= GetPerformanceCounter ();
408 // Count the number of performance counter ticks since the periodic SMI handler
409 // was dispatched or the last time this function was called.
411 if (PeriodicSmiLibraryHandler
->PerfomanceCounterEndValue
> PeriodicSmiLibraryHandler
->PerfomanceCounterStartValue
) {
413 // The performance counter counts up. Check for roll over condition.
415 if (Current
> PeriodicSmiLibraryHandler
->DispatchCheckPointTime
) {
416 Count
= Current
- PeriodicSmiLibraryHandler
->DispatchCheckPointTime
;
418 Count
= (Current
- PeriodicSmiLibraryHandler
->PerfomanceCounterStartValue
) + (PeriodicSmiLibraryHandler
->PerfomanceCounterEndValue
- PeriodicSmiLibraryHandler
->DispatchCheckPointTime
);
422 // The performance counter counts down. Check for roll over condition.
424 if (PeriodicSmiLibraryHandler
->DispatchCheckPointTime
> Current
) {
425 Count
= PeriodicSmiLibraryHandler
->DispatchCheckPointTime
- Current
;
427 Count
= (PeriodicSmiLibraryHandler
->DispatchCheckPointTime
- PeriodicSmiLibraryHandler
->PerfomanceCounterEndValue
) + (PeriodicSmiLibraryHandler
->PerfomanceCounterStartValue
- Current
);
432 // Accumulate the total number of performance counter ticks since the periodic
433 // SMI handler was dispatched or resumed.
435 PeriodicSmiLibraryHandler
->DispatchTotalTime
+= Count
;
438 // Update the checkpoint value to the current performance counter value
440 PeriodicSmiLibraryHandler
->DispatchCheckPointTime
= Current
;
443 // Convert the total number of performance counter ticks to 100 ns units
445 return DivU64x64Remainder (
446 MultU64x32 (PeriodicSmiLibraryHandler
->DispatchTotalTime
, 10000000),
447 PeriodicSmiLibraryHandler
->PerfomanceCounterRate
,
453 This function returns control back to the SMM Foundation. When the next
454 periodic SMI for the currently executing handler is triggered, the periodic
455 SMI handler will restarted from its registered DispatchFunction entry point.
456 If this function is not called from within an enabled periodic SMI handler,
457 then control is returned to the calling function.
466 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
469 // If there is no active periodic SMI handler, then return
471 PeriodicSmiLibraryHandler
= GetActivePeriodicSmiLibraryHandler ();
472 if (PeriodicSmiLibraryHandler
== NULL
) {
477 // Perform a long jump back to the point when the currently executing dispatch
478 // function was dispatched.
480 LongJump (&PeriodicSmiLibraryHandler
->DispatchJumpBuffer
, 1);
490 This function yields control back to the SMM Foundation. When the next
491 periodic SMI for the currently executing handler is triggered, the periodic
492 SMI handler will be resumed and this function will return. Use of this
493 function requires a seperate stack for the periodic SMI handler. A non zero
494 stack size must be specified in PeriodicSmiEnable() for this function to be
497 If the stack size passed into PeriodicSmiEnable() was zero, the 0 is returned.
499 If this function is not called from within an enabled periodic SMI handler,
502 @return The actual time in 100ns units elasped since this function was
503 called. A value of 0 indicates an unknown amount of time.
512 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
516 // If there is no active periodic SMI handler, then return
518 PeriodicSmiLibraryHandler
= GetActivePeriodicSmiLibraryHandler ();
519 if (PeriodicSmiLibraryHandler
== NULL
) {
524 // If PeriodicSmiYield() is called without an allocated stack, then just return
525 // immediately with an elapsed time of 0.
527 if (PeriodicSmiLibraryHandler
->Stack
== NULL
) {
532 // Set a flag so the next periodic SMI event will resume at where SetJump()
535 PeriodicSmiLibraryHandler
->YieldFlag
= TRUE
;
538 // Save context in YieldJumpBuffer
540 SetJumpFlag
= SetJump (&PeriodicSmiLibraryHandler
->YieldJumpBuffer
);
541 if (SetJumpFlag
== 0) {
543 // The intial call to SetJump() always returns 0.
544 // If this is the initial call, then exit the current periodic SMI handler
550 // We get here when a LongJump is performed from PeriodicSmiDispatchFunctionOnCpu()
551 // to resume a periodic SMI handler that called PeriodicSmiYield() on the
552 // previous time this periodic SMI handler was dispatched.
554 // Clear the flag so the next periodic SMI dispatch will not resume.
556 PeriodicSmiLibraryHandler
->YieldFlag
= FALSE
;
559 // Return the amount elapsed time that occured while yielded
561 return PeriodicSmiLibraryHandler
->ElapsedTime
;
565 Internal worker function that transfers control to an enabled periodic SMI
566 handler. If the enabled periodic SMI handler was allocated its own stack,
567 then this function is called on that allocated stack through the BaseLin
568 function SwitchStack().
570 @param[in] Context1 Context1 parameter passed into SwitchStack().
571 @param[in] Context2 Context2 parameter passed into SwitchStack().
576 PeriodicSmiDispatchFunctionSwitchStack (
577 IN VOID
*Context1
, OPTIONAL
578 IN VOID
*Context2 OPTIONAL
581 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
584 // Convert Context1 to PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *
586 PeriodicSmiLibraryHandler
= (PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*)Context1
;
589 // Dispatch the registered handler passing in the context that was registered
590 // and the amount of time that has elapsed since the previous time this
591 // periodic SMI handler was dispacthed.
593 PeriodicSmiLibraryHandler
->DispatchFunction (
594 PeriodicSmiLibraryHandler
->Context
,
595 PeriodicSmiLibraryHandler
->ElapsedTime
599 // If this DispatchFunction() returns, then unconditially call PeriodicSmiExit()
600 // to perform a LongJump() back to PeriodicSmiDispatchFunctionOnCpu(). The
601 // LongJump() will resume exection on the original stack.
607 Internal worker function that transfers control to an enabled periodic SMI
608 handler on the specified logial CPU. This function determines if the periodic
609 SMI handler yielded and needs to be resumed. It also and switches to an
610 allocated stack if one was allocated in PeriodicSmiEnable().
612 @param[in] PeriodicSmiLibraryHandler A pointer to the context for the periodic
613 SMI handler to execute.
618 PeriodicSmiDispatchFunctionOnCpu (
619 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
623 // Save context in DispatchJumpBuffer. The intial call to SetJump() always
624 // returns 0. If this is the initial call, then either resume from a prior
625 // call to PeriodicSmiYield() or call the DispatchFunction registerd in
626 // PeriodicSmiEnable() using an allocated stack if one was specified.
628 if (SetJump (&PeriodicSmiLibraryHandler
->DispatchJumpBuffer
) != 0) {
633 // Capture the performance counter value just before the periodic SMI handler
634 // is resumed so the amount of time the periodic SMI handler executes can be
637 PeriodicSmiLibraryHandler
->DispatchTotalTime
= 0;
638 PeriodicSmiLibraryHandler
->DispatchCheckPointTime
= GetPerformanceCounter();
640 if (PeriodicSmiLibraryHandler
->YieldFlag
) {
642 // Perform a long jump back to the point where the previously dispatched
643 // function called PeriodicSmiYield().
645 LongJump (&PeriodicSmiLibraryHandler
->YieldJumpBuffer
, 1);
646 } else if (PeriodicSmiLibraryHandler
->Stack
== NULL
) {
648 // If Stack is NULL then call DispatchFunction using current stack passing
649 // in the context that was registered and the amount of time that has
650 // elapsed since the previous time this periodic SMI handler was dispacthed.
652 PeriodicSmiLibraryHandler
->DispatchFunction (
653 PeriodicSmiLibraryHandler
->Context
,
654 PeriodicSmiLibraryHandler
->ElapsedTime
658 // If this DispatchFunction() returns, then unconditially call PeriodicSmiExit()
659 // to perform a LongJump() back to this function.
664 // If Stack is not NULL then call DispatchFunction switching to the allocated stack
667 PeriodicSmiDispatchFunctionSwitchStack
,
668 PeriodicSmiLibraryHandler
,
670 (UINT8
*)PeriodicSmiLibraryHandler
->Stack
+ PeriodicSmiLibraryHandler
->StackSize
682 Internal worker function that transfers control to an enabled periodic SMI
683 handler on the specified logial CPU. This worker function is only called
684 using the SMM Services Table function SmmStartupThisAp() to execute the
685 periodic SMI handler on a logical CPU that is different than the one that is
686 running the SMM Foundation. When the periodic SMI handler returns, a lock is
687 released to notify the CPU that is running the SMM Foundation that the periodic
688 SMI handler execution has finished its execution.
690 @param[in] Buffer A pointer to the context for the periodic SMI handler.
695 PeriodicSmiDispatchFunctionWithLock (
699 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
704 PeriodicSmiLibraryHandler
= (PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*)Buffer
;
707 // Execute dispatch function on the currently excuting logical CPU
709 PeriodicSmiDispatchFunctionOnCpu (PeriodicSmiLibraryHandler
);
712 // Release the dispatch spin lock
714 ReleaseSpinLock (&PeriodicSmiLibraryHandler
->DispatchLock
);
718 Internal worker function that transfers control to a periodic SMI handler that
719 was enabled using PeriodicSmiEnable().
721 @param[in] DispatchHandle The unique handle assigned to this handler by
722 SmiHandlerRegister().
723 @param[in] Context Points to an optional handler context which was
724 specified when the handler was registered.
725 @param[in,out] CommBuffer A pointer to a collection of data in memory that
726 will be conveyed from a non-SMM environment into
728 @param[in,out] CommBufferSize The size of the CommBuffer.
730 @retval EFI_SUCCESS The interrupt was handled and quiesced.
731 No other handlers should still be called.
732 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other
733 handlers should still be called.
734 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other
735 handlers should still be called.
736 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
741 PeriodicSmiDispatchFunction (
742 IN EFI_HANDLE DispatchHandle
,
743 IN CONST VOID
*Context OPTIONAL
,
744 IN OUT VOID
*CommBuffer OPTIONAL
,
745 IN OUT UINTN
*CommBufferSize OPTIONAL
748 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
749 EFI_SMM_PERIODIC_TIMER_CONTEXT
*TimerContext
;
753 // Set the active periodic SMI handler
755 PeriodicSmiLibraryHandler
= SetActivePeriodicSmiLibraryHandler (Context
);
756 if (PeriodicSmiLibraryHandler
== NULL
) {
757 return EFI_NOT_FOUND
;
761 // Retrieve the elapsed time since the last time this periodic SMI handler was called
763 PeriodicSmiLibraryHandler
->ElapsedTime
= 0;
764 if (CommBuffer
!= NULL
) {
765 TimerContext
= (EFI_SMM_PERIODIC_TIMER_CONTEXT
*)CommBuffer
;
766 PeriodicSmiLibraryHandler
->ElapsedTime
= TimerContext
->ElapsedTime
;
770 // Dispatch the periodic SMI handler
772 if ((PeriodicSmiLibraryHandler
->Cpu
== PERIODIC_SMI_LIBRARY_ANY_CPU
) ||
773 (PeriodicSmiLibraryHandler
->Cpu
== gSmst
->CurrentlyExecutingCpu
) ) {
775 // Dispatch on the currently execution CPU if the CPU specified in PeriodicSmiEnable()
776 // was PERIODIC_SMI_LIBARRY_ANY_CPU or the currently executing CPU matches the CPU
777 // that was specified in PeriodicSmiEnable().
779 PeriodicSmiDispatchFunctionOnCpu (PeriodicSmiLibraryHandler
);
782 // Acquire spin lock for ths periodic SMI handler. The AP will release the
783 // spin lock when it is done executing the periodic SMI handler.
785 AcquireSpinLock (&PeriodicSmiLibraryHandler
->DispatchLock
);
788 // Execute the periodic SMI handler on the CPU that was specified in
789 // PeriodicSmiEnable().
791 Status
= gSmst
->SmmStartupThisAp (
792 PeriodicSmiDispatchFunctionWithLock
,
793 PeriodicSmiLibraryHandler
->Cpu
,
794 PeriodicSmiLibraryHandler
796 if (!EFI_ERROR (Status
)) {
798 // Wait for the AP to release the spin lock.
800 while (!AcquireSpinLockOrFail (&PeriodicSmiLibraryHandler
->DispatchLock
)) {
806 // Release the spin lock for the periodic SMI handler.
808 ReleaseSpinLock (&PeriodicSmiLibraryHandler
->DispatchLock
);
812 // Retrieve the active periodic SMI handler in case the entries were reallocated
813 // when the active periodic SMI handler was dispatched.
815 PeriodicSmiLibraryHandler
= GetActivePeriodicSmiLibraryHandler ();
816 if (PeriodicSmiLibraryHandler
!= NULL
) {
818 // If the active periodic SMI handler was disabled during the current dispatch
819 // and the periodic SMI handler was allocated a stack when it was enabled, then
820 // free that stack here.
822 if (PeriodicSmiLibraryHandler
->DispatchHandle
== NULL
) {
823 if (PeriodicSmiLibraryHandler
->Stack
!= NULL
) {
825 PeriodicSmiLibraryHandler
->Stack
,
826 EFI_SIZE_TO_PAGES (PeriodicSmiLibraryHandler
->StackSize
)
828 PeriodicSmiLibraryHandler
->Stack
= NULL
;
834 // Update state to show that there is no active periodic SMI handler
836 SetActivePeriodicSmiLibraryHandler (NULL
);
842 This function enables a periodic SMI handler.
844 @param[in,out] DispatchHandle A pointer to the handle associated with the
845 enabled periodic SMI handler. This is an
846 optional parameter that may be NULL. If it is
847 NULL, then the handle will not be returned,
848 which means that the periodic SMI handler can
850 @param[in] DispatchFunction A pointer to a periodic SMI handler function.
851 @param[in] Context Optional content to pass into DispatchFunction.
852 @param[in] TickPeriod The requested tick period in 100ns units that
853 control should be givien to the periodic SMI
854 handler. Must be one of the supported values
855 returned by PeriodicSmiSupportedPickPeriod().
856 @param[in] Cpu Specifies the CPU that is required to execute
857 the periodic SMI handler. If Cpu is
858 PERIODIC_SMI_LIBRARY_ANY_CPU, then the periodic
859 SMI handler will always be executed on the SMST
860 CurrentlyExecutingCpu, which may vary across
861 periodic SMIs. If Cpu is between 0 and the SMST
862 NumberOfCpus, then the periodic SMI will always
863 be executed on the requested CPU.
864 @param[in] StackSize The size, in bytes, of the stack to allocate for
865 use by the periodic SMI handler. If 0, then the
866 default stack will be used.
868 @retval EFI_INVALID_PARAMETER DispatchFunction is NULL.
869 @retval EFI_UNSUPPORTED TickPeriod is not a supported tick period. The
870 supported tick periods can be retrieved using
871 PeriodicSmiSupportedTickPeriod().
872 @retval EFI_INVALID_PARAMETER Cpu is not PERIODIC_SMI_LIBRARY_ANY_CPU or in
873 the range 0 to SMST NumberOfCpus.
874 @retval EFI_OUT_OF_RESOURCES There are not enough resources to enable the
875 periodic SMI handler.
876 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate the
877 stack speficied by StackSize.
878 @retval EFI_SUCCESS The periodic SMI handler was enabled.
884 IN OUT EFI_HANDLE
*DispatchHandle
, OPTIONAL
885 IN PERIODIC_SMI_LIBRARY_HANDLER DispatchFunction
,
886 IN CONST VOID
*Context
, OPTIONAL
887 IN UINT64 TickPeriod
,
894 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
897 // Make sure all the input parameters are valid
899 if (DispatchFunction
== NULL
) {
900 return EFI_INVALID_PARAMETER
;
903 for (Index
= 0; gSmiTickPeriodTable
[Index
] != 0; Index
++) {
904 if (gSmiTickPeriodTable
[Index
] == TickPeriod
) {
908 if (gSmiTickPeriodTable
[Index
] == 0) {
909 return EFI_UNSUPPORTED
;
912 if (Cpu
!= PERIODIC_SMI_LIBRARY_ANY_CPU
&& Cpu
>= gSmst
->NumberOfCpus
) {
913 return EFI_INVALID_PARAMETER
;
917 // Find a free periodic SMI handler entry
919 PeriodicSmiLibraryHandler
= FindFreePeriodicSmiLibraryHandler();
920 if (PeriodicSmiLibraryHandler
== NULL
) {
921 return EFI_OUT_OF_RESOURCES
;
925 // Initialize a new periodic SMI handler entry
927 PeriodicSmiLibraryHandler
->Signature
= PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE
;
928 PeriodicSmiLibraryHandler
->YieldFlag
= FALSE
;
929 PeriodicSmiLibraryHandler
->DispatchHandle
= NULL
;
930 PeriodicSmiLibraryHandler
->DispatchFunction
= DispatchFunction
;
931 PeriodicSmiLibraryHandler
->Context
= (VOID
*)Context
;
932 PeriodicSmiLibraryHandler
->Cpu
= Cpu
;
933 PeriodicSmiLibraryHandler
->StackSize
= ALIGN_VALUE (StackSize
, EFI_PAGE_SIZE
);
934 if (PeriodicSmiLibraryHandler
->StackSize
> 0) {
935 PeriodicSmiLibraryHandler
->Stack
= AllocatePages (EFI_SIZE_TO_PAGES (PeriodicSmiLibraryHandler
->StackSize
));
936 if (PeriodicSmiLibraryHandler
->Stack
== NULL
) {
937 return EFI_OUT_OF_RESOURCES
;
939 ZeroMem (PeriodicSmiLibraryHandler
->Stack
, PeriodicSmiLibraryHandler
->StackSize
);
941 InitializeSpinLock (&PeriodicSmiLibraryHandler
->DispatchLock
);
942 PeriodicSmiLibraryHandler
->PerfomanceCounterRate
= GetPerformanceCounterProperties (
943 &PeriodicSmiLibraryHandler
->PerfomanceCounterStartValue
,
944 &PeriodicSmiLibraryHandler
->PerfomanceCounterEndValue
946 PeriodicSmiLibraryHandler
->RegisterContext
.Period
= TickPeriod
;
947 PeriodicSmiLibraryHandler
->RegisterContext
.SmiTickInterval
= TickPeriod
;
948 Status
= gSmmPeriodicTimerDispatch2
->Register (
949 gSmmPeriodicTimerDispatch2
,
950 PeriodicSmiDispatchFunction
,
951 &PeriodicSmiLibraryHandler
->RegisterContext
,
952 &PeriodicSmiLibraryHandler
->DispatchHandle
954 if (EFI_ERROR (Status
) || PeriodicSmiLibraryHandler
->DispatchHandle
== NULL
) {
956 // If the registration failed or the handle is invalid, free the stack if one was allocated
958 if (PeriodicSmiLibraryHandler
->Stack
!= NULL
) {
960 PeriodicSmiLibraryHandler
->Stack
,
961 EFI_SIZE_TO_PAGES (PeriodicSmiLibraryHandler
->StackSize
)
963 PeriodicSmiLibraryHandler
->Stack
= NULL
;
965 PeriodicSmiLibraryHandler
->DispatchHandle
= NULL
;
966 return EFI_OUT_OF_RESOURCES
;
970 // Return the registered handle if the optional DispatchHandle parameter is not NULL
972 if (DispatchHandle
!= NULL
) {
973 *DispatchHandle
= PeriodicSmiLibraryHandler
->DispatchHandle
;
979 This function disables a periodic SMI handler that has been previously
980 enabled with PeriodicSmiEnable().
982 @param[in] DispatchHandle A handle associated with a previously enabled periodic
983 SMI handler. This is an optional parameter that may
984 be NULL. If it is NULL, then the active periodic SMI
985 handlers is disabled.
987 @retval FALSE DispatchHandle is NULL and there is no active periodic SMI handler.
988 @retval FALSE The periodic SMI handler specified by DispatchHandle has
989 not been enabled with PeriodicSmiEnable().
990 @retval TRUE The periodic SMI handler specified by DispatchHandle has
991 been disabled. If DispatchHandle is NULL, then the active
992 periodic SMI handler has been disabled.
998 IN EFI_HANDLE DispatchHandle OPTIONAL
1001 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
1005 // Lookup the periodic SMI handler specified by DispatchHandle
1007 PeriodicSmiLibraryHandler
= LookupPeriodicSmiLibraryHandler (DispatchHandle
);
1008 if (PeriodicSmiLibraryHandler
== NULL
) {
1013 // Unregister the periodic SMI handler from the SMM Periodic Timer Dispatch 2 Protocol
1015 Status
= gSmmPeriodicTimerDispatch2
->UnRegister (
1016 gSmmPeriodicTimerDispatch2
,
1017 PeriodicSmiLibraryHandler
->DispatchHandle
1019 if (EFI_ERROR (Status
)) {
1024 // If active periodic SMI handler is not the periodic SMI handler being
1025 // disabled, and the periodic SMI handler being disabled was allocated a
1026 // stack when it was enabled, then free the stack.
1028 if (PeriodicSmiLibraryHandler
!= GetActivePeriodicSmiLibraryHandler ()) {
1029 if (PeriodicSmiLibraryHandler
->Stack
!= NULL
) {
1031 PeriodicSmiLibraryHandler
->Stack
,
1032 EFI_SIZE_TO_PAGES (PeriodicSmiLibraryHandler
->StackSize
)
1034 PeriodicSmiLibraryHandler
->Stack
= NULL
;
1039 // Mark the entry for the disabled periodic SMI handler as free
1041 PeriodicSmiLibraryHandler
->DispatchHandle
= NULL
;
1047 This constructor function caches the pointer to the SMM Periodic Timer
1048 Dispatch 2 Protocol and collects the list SMI tick rates that the hardware
1051 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1052 @param[in] SystemTable A pointer to the EFI System Table.
1054 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
1059 SmmPeriodicSmiLibConstructor (
1060 IN EFI_HANDLE ImageHandle
,
1061 IN EFI_SYSTEM_TABLE
*SystemTable
1065 UINT64
*SmiTickInterval
;
1069 // Locate the SMM Periodic Timer Dispatch 2 Protocol
1071 Status
= gSmst
->SmmLocateProtocol (
1072 &gEfiSmmPeriodicTimerDispatch2ProtocolGuid
,
1074 (VOID
**)&gSmmPeriodicTimerDispatch2
1076 ASSERT_EFI_ERROR (Status
);
1077 ASSERT (gSmmPeriodicTimerDispatch2
!= NULL
);
1080 // Count the number of periodic SMI tick intervals that the SMM Periodic Timer
1081 // Dipatch 2 Protocol supports.
1083 SmiTickInterval
= NULL
;
1086 Status
= gSmmPeriodicTimerDispatch2
->GetNextShorterInterval (
1087 gSmmPeriodicTimerDispatch2
,
1091 } while (SmiTickInterval
!= NULL
);
1094 // Allocate a buffer for the table of supported periodic SMI tick periods.
1096 gSmiTickPeriodTable
= AllocateZeroPool (Count
* sizeof (UINT64
));
1097 ASSERT (gSmiTickPeriodTable
!= NULL
);
1100 // Fill in the table of supported periodic SMI tick periods.
1102 SmiTickInterval
= NULL
;
1105 gSmiTickPeriodTable
[Count
] = 0;
1106 Status
= gSmmPeriodicTimerDispatch2
->GetNextShorterInterval (
1107 gSmmPeriodicTimerDispatch2
,
1110 if (SmiTickInterval
!= NULL
) {
1111 gSmiTickPeriodTable
[Count
] = *SmiTickInterval
;
1114 } while (SmiTickInterval
!= NULL
);
1117 // Allocate buffer for initial set of periodic SMI handlers
1119 FindFreePeriodicSmiLibraryHandler ();
1125 The constructor function caches the pointer to the SMM Periodic Timer Dispatch 2
1126 Protocol and collects the list SMI tick rates that the hardware supports.
1128 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1129 @param[in] SystemTable A pointer to the EFI System Table.
1131 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
1136 SmmPeriodicSmiLibDestructor (
1137 IN EFI_HANDLE ImageHandle
,
1138 IN EFI_SYSTEM_TABLE
*SystemTable
1144 // Free the table of supported periodic SMI tick rates
1146 if (gSmiTickPeriodTable
!= NULL
) {
1147 FreePool (gSmiTickPeriodTable
);
1151 // Disable all periodic SMI handlers
1153 for (Index
= 0; Index
< gNumberOfPeriodicSmiLibraryHandlers
; Index
++) {
1154 PeriodicSmiDisable (gPeriodicSmiLibraryHandlers
[Index
].DispatchHandle
);
1158 // Free all the periodic SMI handler entries
1160 if (gPeriodicSmiLibraryHandlers
!= NULL
) {
1161 FreePool (gPeriodicSmiLibraryHandlers
);