2 SMM Periodic SMI Library.
4 Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Protocol/SmmPeriodicTimerDispatch2.h>
13 #include <Library/BaseLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/SynchronizationLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/TimerLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/SmmServicesTableLib.h>
21 #include <Library/SmmPeriodicSmiLib.h>
24 /// Define the number of periodic SMI handler entries that should be allocated to the list
25 /// of free periodic SMI handlers when the list of free periodic SMI handlers is empty.
27 #define PERIODIC_SMI_LIBRARY_ALLOCATE_SIZE 0x08
30 /// Signature for a PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure
32 #define PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE SIGNATURE_32 ('P', 'S', 'M', 'I')
35 /// Structure that contains state information for an enabled periodic SMI handler
39 /// Signature value that must be set to PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE
43 /// The link entry to be inserted to the list of periodic SMI handlers.
47 /// The dispatch function to called to invoke an enabled periodic SMI handler.
49 PERIODIC_SMI_LIBRARY_HANDLER DispatchFunction
;
51 /// The context to pass into DispatchFunction
55 /// The tick period in 100 ns units that DispatchFunction should be called.
59 /// The Cpu number that is required to execute DispatchFunction. If Cpu is
60 /// set to PERIODIC_SMI_LIBRARY_ANY_CPU, then DispatchFunction may be executed
65 /// The size, in bytes, of the stack allocated for a periodic SMI handler.
66 /// This value must be a multiple of EFI_PAGE_SIZE.
70 /// A pointer to the stack allocated using AllocatePages(). This field will
71 /// be NULL if StackSize is 0.
75 /// Spin lock used to wait for an AP to complete the execution of a periodic SMI handler
77 SPIN_LOCK DispatchLock
;
79 /// The rate in Hz of the performance counter that is used to measure the
80 /// amount of time that a periodic SMI handler executes.
82 UINT64 PerfomanceCounterRate
;
84 /// The start count value of the performance counter that is used to measure
85 /// the amount of time that a periodic SMI handler executes.
87 UINT64 PerfomanceCounterStartValue
;
89 /// The end count value of the performance counter that is used to measure
90 /// the amount of time that a periodic SMI handler executes.
92 UINT64 PerfomanceCounterEndValue
;
94 /// The context record passed into the Register() function of the SMM Periodic
95 /// Timer Dispatch Protocol when a periodic SMI handler is enabled.
97 EFI_SMM_PERIODIC_TIMER_REGISTER_CONTEXT RegisterContext
;
99 /// The handle returned from the Register() function of the SMM Periodic
100 /// Timer Dispatch Protocol when a periodic SMI handler is enabled.
102 EFI_HANDLE DispatchHandle
;
104 /// The total number of performance counter ticks that the periodic SMI handler
105 /// has been executing in its current invocation.
107 UINT64 DispatchTotalTime
;
109 /// The performance counter value that was captured the last time that the
110 /// periodic SMI handler called PeriodicSmiExecutionTime(). This allows the
111 /// time value returned by PeriodicSmiExecutionTime() to be accurate even when
112 /// the performance counter rolls over.
114 UINT64 DispatchCheckPointTime
;
116 /// Buffer used to save the context when control is transfer from this library
117 /// to an enabled periodic SMI handler. This saved context is used when the
118 /// periodic SMI handler exits or yields.
120 BASE_LIBRARY_JUMP_BUFFER DispatchJumpBuffer
;
122 /// Flag that is set to TRUE when a periodic SMI handler requests to yield
123 /// using PeriodicSmiYield(). When this flag IS TRUE, YieldJumpBuffer is
124 /// valid. When this flag is FALSE, YieldJumpBuffer is not valid.
128 /// Buffer used to save the context when a periodic SMI handler requests to
129 /// yield using PeriodicSmiYield(). This context is used to resume the
130 /// execution of a periodic SMI handler the next time control is transferred
131 /// to the periodic SMI handler that yielded.
133 BASE_LIBRARY_JUMP_BUFFER YieldJumpBuffer
;
135 /// The amount of time, in 100 ns units, that have elapsed since the last
136 /// time the periodic SMI handler was invoked.
139 } PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
;
142 Macro that returns a pointer to a PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
143 structure based on a pointer to a Link field.
146 #define PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_LINK(a) \
149 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT, \
151 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE \
155 /// Pointer to the SMM Periodic Timer Dispatch Protocol that was located in the constructor.
157 EFI_SMM_PERIODIC_TIMER_DISPATCH2_PROTOCOL
*gSmmPeriodicTimerDispatch2
= NULL
;
160 /// Pointer to a table of supported periodic SMI tick periods in 100 ns units
161 /// sorted from largest to smallest terminated by a tick period value of 0.
162 /// This table is allocated using AllocatePool() in the constructor and filled
163 /// in based on the values returned from the SMM Periodic Timer Dispatch 2 Protocol
164 /// function GetNextShorterInterval().
166 UINT64
*gSmiTickPeriodTable
= NULL
;
169 /// Linked list of free periodic SMI handlers that this library can use.
171 LIST_ENTRY gFreePeriodicSmiLibraryHandlers
=
172 INITIALIZE_LIST_HEAD_VARIABLE (gFreePeriodicSmiLibraryHandlers
);
175 /// Linked list of periodic SMI handlers that this library is currently managing.
177 LIST_ENTRY gPeriodicSmiLibraryHandlers
=
178 INITIALIZE_LIST_HEAD_VARIABLE (gPeriodicSmiLibraryHandlers
);
181 /// Pointer to the periodic SMI handler that is currently being executed.
182 /// Is set to NULL if no periodic SMI handler is currently being executed.
184 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*gActivePeriodicSmiLibraryHandler
= NULL
;
187 Internal worker function that returns a pointer to the
188 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure associated with the periodic
189 SMI handler that is currently being executed. If a periodic SMI handler is
190 not currently being executed, the NULL is returned.
192 @retval NULL A periodic SMI handler is not currently being executed.
193 @retval other Pointer to the PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
194 associated with the active periodic SMI handler.
197 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*
198 GetActivePeriodicSmiLibraryHandler (
202 return gActivePeriodicSmiLibraryHandler
;
206 Internal worker function that returns a pointer to the
207 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure associated with the
208 DispatchHandle that was returned when the periodic SMI handler was enabled
209 with PeriodicSmiEnable(). If DispatchHandle is NULL, then the active
210 periodic SMI handler is returned. If DispatchHandle is NULL and there is
211 no active periodic SMI handler, then NULL is returned.
213 @param[in] DispatchHandle DispatchHandle that was returned when the periodic
214 SMI handler was enabled with PeriodicSmiEnable().
215 This is an optional parameter that may be NULL.
216 If this parameter is NULL, then the active periodic
217 SMI handler is returned.
219 @retval NULL DispatchHandle is NULL and there is no active periodic SMI
221 @retval NULL DispatchHandle does not match any of the periodic SMI handlers
222 that have been enabled with PeriodicSmiEnable().
223 @retval other Pointer to the PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
224 associated with the DispatchHandle.
227 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*
228 LookupPeriodicSmiLibraryHandler (
229 IN EFI_HANDLE DispatchHandle OPTIONAL
233 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
236 // If DispatchHandle is NULL, then return the active periodic SMI handler
238 if (DispatchHandle
== NULL
) {
239 return GetActivePeriodicSmiLibraryHandler ();
243 // Search the periodic SMI handler entries for a a matching DispatchHandle
245 for ( Link
= GetFirstNode (&gPeriodicSmiLibraryHandlers
)
246 ; !IsNull (&gPeriodicSmiLibraryHandlers
, Link
)
247 ; Link
= GetNextNode (&gPeriodicSmiLibraryHandlers
, Link
)
250 PeriodicSmiLibraryHandler
= PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_LINK (Link
);
252 if (PeriodicSmiLibraryHandler
->DispatchHandle
== DispatchHandle
) {
253 return PeriodicSmiLibraryHandler
;
258 // No entries match DispatchHandle, so return NULL
264 Internal worker function that sets that active periodic SMI handler based on
265 the DispatchHandle that was returned when the periodic SMI handler was enabled
266 with PeriodicSmiEnable(). If DispatchHandle is NULL, then the
267 state is updated to show that there is not active periodic SMI handler.
268 A pointer to the active PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure
271 @param [in] DispatchHandle DispatchHandle that was returned when the periodic
272 SMI handler was enabled with PeriodicSmiEnable().
273 This is an optional parameter that may be NULL.
274 If this parameter is NULL, then the state is updated
275 to show that there is not active periodic SMI handler.
276 @retval NULL DispatchHandle is NULL.
277 @retval other Pointer to the PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
278 associated with DispatchHandle.
281 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*
282 SetActivePeriodicSmiLibraryHandler (
283 IN EFI_HANDLE DispatchHandle OPTIONAL
286 if (DispatchHandle
== NULL
) {
287 gActivePeriodicSmiLibraryHandler
= NULL
;
289 gActivePeriodicSmiLibraryHandler
= LookupPeriodicSmiLibraryHandler (DispatchHandle
);
292 return gActivePeriodicSmiLibraryHandler
;
296 Internal worker function that moves the specified periodic SMI handler from the
297 list of managed periodic SMI handlers to the list of free periodic SMI handlers.
299 @param[in] PeriodicSmiLibraryHandler Pointer to the periodic SMI handler to be reclaimed.
302 ReclaimPeriodicSmiLibraryHandler (
303 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
306 ASSERT (PeriodicSmiLibraryHandler
->DispatchHandle
== NULL
);
307 if (PeriodicSmiLibraryHandler
->Stack
!= NULL
) {
309 PeriodicSmiLibraryHandler
->Stack
,
310 EFI_SIZE_TO_PAGES (PeriodicSmiLibraryHandler
->StackSize
)
312 PeriodicSmiLibraryHandler
->Stack
= NULL
;
315 RemoveEntryList (&PeriodicSmiLibraryHandler
->Link
);
316 InsertHeadList (&gFreePeriodicSmiLibraryHandlers
, &PeriodicSmiLibraryHandler
->Link
);
320 Add the additional entries to the list of free periodic SMI handlers.
321 The function is assumed to be called only when the list of free periodic SMI
324 @retval TRUE The additional entries were added.
325 @retval FALSE There was no available resource for the additional entries.
328 EnlargeFreePeriodicSmiLibraryHandlerList (
333 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
336 // Add the entries to the list
338 for (Index
= 0; Index
< PERIODIC_SMI_LIBRARY_ALLOCATE_SIZE
; Index
++) {
339 PeriodicSmiLibraryHandler
= AllocatePool (sizeof (PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
));
340 if (PeriodicSmiLibraryHandler
== NULL
) {
344 PeriodicSmiLibraryHandler
->Signature
= PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE
;
345 InsertHeadList (&gFreePeriodicSmiLibraryHandlers
, &PeriodicSmiLibraryHandler
->Link
);
348 return (BOOLEAN
)(Index
> 0);
352 Internal worker function that returns a free entry for a new periodic
353 SMI handler. If no free entries are available, then additional
354 entries are allocated.
356 @retval NULL There are not enough resources available to to allocate a free entry.
357 @retval other Pointer to a free PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure.
360 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*
361 FindFreePeriodicSmiLibraryHandler (
365 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
367 if (IsListEmpty (&gFreePeriodicSmiLibraryHandlers
)) {
368 if (!EnlargeFreePeriodicSmiLibraryHandlerList ()) {
374 // Get one from the list of free periodic SMI handlers.
376 PeriodicSmiLibraryHandler
= PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_LINK (
377 GetFirstNode (&gFreePeriodicSmiLibraryHandlers
)
379 RemoveEntryList (&PeriodicSmiLibraryHandler
->Link
);
380 InsertTailList (&gPeriodicSmiLibraryHandlers
, &PeriodicSmiLibraryHandler
->Link
);
382 return PeriodicSmiLibraryHandler
;
386 This function returns a pointer to a table of supported periodic
387 SMI tick periods in 100 ns units sorted from largest to smallest.
388 The table contains a array of UINT64 values terminated by a tick
389 period value of 0. The returned table must be treated as read-only
390 data and must not be freed.
392 @return A pointer to a table of UINT64 tick period values in
393 100ns units sorted from largest to smallest terminated
394 by a tick period of 0.
399 PeriodicSmiSupportedTickPeriod (
404 // Return the table allocated and populated by SmmPeriodicSmiLibConstructor()
406 return gSmiTickPeriodTable
;
410 This function returns the time in 100ns units since the periodic SMI
411 handler function was called. If the periodic SMI handler was resumed
412 through PeriodicSmiYield(), then the time returned is the time in
413 100ns units since PeriodicSmiYield() returned.
415 @return The actual time in 100ns units that the periodic SMI handler
416 has been executing. If this function is not called from within
417 an enabled periodic SMI handler, then 0 is returned.
422 PeriodicSmiExecutionTime (
426 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
431 // If there is no active periodic SMI handler, then return 0
433 PeriodicSmiLibraryHandler
= GetActivePeriodicSmiLibraryHandler ();
434 if (PeriodicSmiLibraryHandler
== NULL
) {
439 // Get the current performance counter value
441 Current
= GetPerformanceCounter ();
444 // Count the number of performance counter ticks since the periodic SMI handler
445 // was dispatched or the last time this function was called.
447 if (PeriodicSmiLibraryHandler
->PerfomanceCounterEndValue
> PeriodicSmiLibraryHandler
->PerfomanceCounterStartValue
) {
449 // The performance counter counts up. Check for roll over condition.
451 if (Current
> PeriodicSmiLibraryHandler
->DispatchCheckPointTime
) {
452 Count
= Current
- PeriodicSmiLibraryHandler
->DispatchCheckPointTime
;
454 Count
= (Current
- PeriodicSmiLibraryHandler
->PerfomanceCounterStartValue
) + (PeriodicSmiLibraryHandler
->PerfomanceCounterEndValue
- PeriodicSmiLibraryHandler
->DispatchCheckPointTime
);
458 // The performance counter counts down. Check for roll over condition.
460 if (PeriodicSmiLibraryHandler
->DispatchCheckPointTime
> Current
) {
461 Count
= PeriodicSmiLibraryHandler
->DispatchCheckPointTime
- Current
;
463 Count
= (PeriodicSmiLibraryHandler
->DispatchCheckPointTime
- PeriodicSmiLibraryHandler
->PerfomanceCounterEndValue
) + (PeriodicSmiLibraryHandler
->PerfomanceCounterStartValue
- Current
);
468 // Accumulate the total number of performance counter ticks since the periodic
469 // SMI handler was dispatched or resumed.
471 PeriodicSmiLibraryHandler
->DispatchTotalTime
+= Count
;
474 // Update the checkpoint value to the current performance counter value
476 PeriodicSmiLibraryHandler
->DispatchCheckPointTime
= Current
;
479 // Convert the total number of performance counter ticks to 100 ns units
481 return DivU64x64Remainder (
482 MultU64x32 (PeriodicSmiLibraryHandler
->DispatchTotalTime
, 10000000),
483 PeriodicSmiLibraryHandler
->PerfomanceCounterRate
,
489 This function returns control back to the SMM Foundation. When the next
490 periodic SMI for the currently executing handler is triggered, the periodic
491 SMI handler will restarted from its registered DispatchFunction entry point.
492 If this function is not called from within an enabled periodic SMI handler,
493 then control is returned to the calling function.
502 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
505 // If there is no active periodic SMI handler, then return
507 PeriodicSmiLibraryHandler
= GetActivePeriodicSmiLibraryHandler ();
508 if (PeriodicSmiLibraryHandler
== NULL
) {
513 // Perform a long jump back to the point when the currently executing dispatch
514 // function was dispatched.
516 LongJump (&PeriodicSmiLibraryHandler
->DispatchJumpBuffer
, 1);
526 This function yields control back to the SMM Foundation. When the next
527 periodic SMI for the currently executing handler is triggered, the periodic
528 SMI handler will be resumed and this function will return. Use of this
529 function requires a separate stack for the periodic SMI handler. A non zero
530 stack size must be specified in PeriodicSmiEnable() for this function to be
533 If the stack size passed into PeriodicSmiEnable() was zero, the 0 is returned.
535 If this function is not called from within an enabled periodic SMI handler,
538 @return The actual time in 100ns units elapsed since this function was
539 called. A value of 0 indicates an unknown amount of time.
548 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
552 // If there is no active periodic SMI handler, then return
554 PeriodicSmiLibraryHandler
= GetActivePeriodicSmiLibraryHandler ();
555 if (PeriodicSmiLibraryHandler
== NULL
) {
560 // If PeriodicSmiYield() is called without an allocated stack, then just return
561 // immediately with an elapsed time of 0.
563 if (PeriodicSmiLibraryHandler
->Stack
== NULL
) {
568 // Set a flag so the next periodic SMI event will resume at where SetJump()
571 PeriodicSmiLibraryHandler
->YieldFlag
= TRUE
;
574 // Save context in YieldJumpBuffer
576 SetJumpFlag
= SetJump (&PeriodicSmiLibraryHandler
->YieldJumpBuffer
);
577 if (SetJumpFlag
== 0) {
579 // The initial call to SetJump() always returns 0.
580 // If this is the initial call, then exit the current periodic SMI handler
586 // We get here when a LongJump is performed from PeriodicSmiDispatchFunctionOnCpu()
587 // to resume a periodic SMI handler that called PeriodicSmiYield() on the
588 // previous time this periodic SMI handler was dispatched.
590 // Clear the flag so the next periodic SMI dispatch will not resume.
592 PeriodicSmiLibraryHandler
->YieldFlag
= FALSE
;
595 // Return the amount elapsed time that occurred while yielded
597 return PeriodicSmiLibraryHandler
->ElapsedTime
;
601 Internal worker function that transfers control to an enabled periodic SMI
602 handler. If the enabled periodic SMI handler was allocated its own stack,
603 then this function is called on that allocated stack through the BaseLin
604 function SwitchStack().
606 @param[in] Context1 Context1 parameter passed into SwitchStack().
607 @param[in] Context2 Context2 parameter passed into SwitchStack().
612 PeriodicSmiDispatchFunctionSwitchStack (
613 IN VOID
*Context1 OPTIONAL
,
614 IN VOID
*Context2 OPTIONAL
617 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
620 // Convert Context1 to PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *
622 PeriodicSmiLibraryHandler
= (PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*)Context1
;
625 // Dispatch the registered handler passing in the context that was registered
626 // and the amount of time that has elapsed since the previous time this
627 // periodic SMI handler was dispatched.
629 PeriodicSmiLibraryHandler
->DispatchFunction (
630 PeriodicSmiLibraryHandler
->Context
,
631 PeriodicSmiLibraryHandler
->ElapsedTime
635 // If this DispatchFunction() returns, then unconditionally call PeriodicSmiExit()
636 // to perform a LongJump() back to PeriodicSmiDispatchFunctionOnCpu(). The
637 // LongJump() will resume execution on the original stack.
643 Internal worker function that transfers control to an enabled periodic SMI
644 handler on the specified logical CPU. This function determines if the periodic
645 SMI handler yielded and needs to be resumed. It also and switches to an
646 allocated stack if one was allocated in PeriodicSmiEnable().
648 @param[in] PeriodicSmiLibraryHandler A pointer to the context for the periodic
649 SMI handler to execute.
654 PeriodicSmiDispatchFunctionOnCpu (
655 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
659 // Save context in DispatchJumpBuffer. The initial call to SetJump() always
660 // returns 0. If this is the initial call, then either resume from a prior
661 // call to PeriodicSmiYield() or call the DispatchFunction registered in
662 // PeriodicSmiEnable() using an allocated stack if one was specified.
664 if (SetJump (&PeriodicSmiLibraryHandler
->DispatchJumpBuffer
) != 0) {
669 // Capture the performance counter value just before the periodic SMI handler
670 // is resumed so the amount of time the periodic SMI handler executes can be
673 PeriodicSmiLibraryHandler
->DispatchTotalTime
= 0;
674 PeriodicSmiLibraryHandler
->DispatchCheckPointTime
= GetPerformanceCounter ();
676 if (PeriodicSmiLibraryHandler
->YieldFlag
) {
678 // Perform a long jump back to the point where the previously dispatched
679 // function called PeriodicSmiYield().
681 LongJump (&PeriodicSmiLibraryHandler
->YieldJumpBuffer
, 1);
682 } else if (PeriodicSmiLibraryHandler
->Stack
== NULL
) {
684 // If Stack is NULL then call DispatchFunction using current stack passing
685 // in the context that was registered and the amount of time that has
686 // elapsed since the previous time this periodic SMI handler was dispatched.
688 PeriodicSmiLibraryHandler
->DispatchFunction (
689 PeriodicSmiLibraryHandler
->Context
,
690 PeriodicSmiLibraryHandler
->ElapsedTime
694 // If this DispatchFunction() returns, then unconditionally call PeriodicSmiExit()
695 // to perform a LongJump() back to this function.
700 // If Stack is not NULL then call DispatchFunction switching to the allocated stack
703 PeriodicSmiDispatchFunctionSwitchStack
,
704 PeriodicSmiLibraryHandler
,
706 (UINT8
*)PeriodicSmiLibraryHandler
->Stack
+ PeriodicSmiLibraryHandler
->StackSize
718 Internal worker function that transfers control to an enabled periodic SMI
719 handler on the specified logical CPU. This worker function is only called
720 using the SMM Services Table function SmmStartupThisAp() to execute the
721 periodic SMI handler on a logical CPU that is different than the one that is
722 running the SMM Foundation. When the periodic SMI handler returns, a lock is
723 released to notify the CPU that is running the SMM Foundation that the periodic
724 SMI handler execution has finished its execution.
726 @param[in, out] Buffer A pointer to the context for the periodic SMI handler.
731 PeriodicSmiDispatchFunctionWithLock (
735 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
740 PeriodicSmiLibraryHandler
= (PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*)Buffer
;
743 // Execute dispatch function on the currently executing logical CPU
745 PeriodicSmiDispatchFunctionOnCpu (PeriodicSmiLibraryHandler
);
748 // Release the dispatch spin lock
750 ReleaseSpinLock (&PeriodicSmiLibraryHandler
->DispatchLock
);
754 Internal worker function that transfers control to a periodic SMI handler that
755 was enabled using PeriodicSmiEnable().
757 @param[in] DispatchHandle The unique handle assigned to this handler by
758 SmiHandlerRegister().
759 @param[in] Context Points to an optional handler context which was
760 specified when the handler was registered.
761 @param[in, out] CommBuffer A pointer to a collection of data in memory that
762 will be conveyed from a non-SMM environment into
764 @param[in, out] CommBufferSize The size of the CommBuffer.
766 @retval EFI_SUCCESS The interrupt was handled and quiesced.
767 No other handlers should still be called.
768 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other
769 handlers should still be called.
770 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other
771 handlers should still be called.
772 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
777 PeriodicSmiDispatchFunction (
778 IN EFI_HANDLE DispatchHandle
,
779 IN CONST VOID
*Context OPTIONAL
,
780 IN OUT VOID
*CommBuffer OPTIONAL
,
781 IN OUT UINTN
*CommBufferSize OPTIONAL
784 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
785 EFI_SMM_PERIODIC_TIMER_CONTEXT
*TimerContext
;
789 // Set the active periodic SMI handler
791 PeriodicSmiLibraryHandler
= SetActivePeriodicSmiLibraryHandler (DispatchHandle
);
792 if (PeriodicSmiLibraryHandler
== NULL
) {
793 return EFI_NOT_FOUND
;
797 // Retrieve the elapsed time since the last time this periodic SMI handler was called
799 PeriodicSmiLibraryHandler
->ElapsedTime
= 0;
800 if (CommBuffer
!= NULL
) {
801 TimerContext
= (EFI_SMM_PERIODIC_TIMER_CONTEXT
*)CommBuffer
;
802 PeriodicSmiLibraryHandler
->ElapsedTime
= TimerContext
->ElapsedTime
;
806 // Dispatch the periodic SMI handler
808 if ((PeriodicSmiLibraryHandler
->Cpu
== PERIODIC_SMI_LIBRARY_ANY_CPU
) ||
809 (PeriodicSmiLibraryHandler
->Cpu
== gSmst
->CurrentlyExecutingCpu
))
812 // Dispatch on the currently execution CPU if the CPU specified in PeriodicSmiEnable()
813 // was PERIODIC_SMI_LIBRARY_ANY_CPU or the currently executing CPU matches the CPU
814 // that was specified in PeriodicSmiEnable().
816 PeriodicSmiDispatchFunctionOnCpu (PeriodicSmiLibraryHandler
);
819 // Acquire spin lock for ths periodic SMI handler. The AP will release the
820 // spin lock when it is done executing the periodic SMI handler.
822 AcquireSpinLock (&PeriodicSmiLibraryHandler
->DispatchLock
);
825 // Execute the periodic SMI handler on the CPU that was specified in
826 // PeriodicSmiEnable().
828 Status
= gSmst
->SmmStartupThisAp (
829 PeriodicSmiDispatchFunctionWithLock
,
830 PeriodicSmiLibraryHandler
->Cpu
,
831 PeriodicSmiLibraryHandler
833 if (!EFI_ERROR (Status
)) {
835 // Wait for the AP to release the spin lock.
837 while (!AcquireSpinLockOrFail (&PeriodicSmiLibraryHandler
->DispatchLock
)) {
843 // Release the spin lock for the periodic SMI handler.
845 ReleaseSpinLock (&PeriodicSmiLibraryHandler
->DispatchLock
);
849 // Reclaim the active periodic SMI handler if it was disabled during the current dispatch.
851 if (PeriodicSmiLibraryHandler
->DispatchHandle
== NULL
) {
852 ReclaimPeriodicSmiLibraryHandler (PeriodicSmiLibraryHandler
);
856 // Update state to show that there is no active periodic SMI handler
858 SetActivePeriodicSmiLibraryHandler (NULL
);
864 This function enables a periodic SMI handler.
866 @param[in, out] DispatchHandle A pointer to the handle associated with the
867 enabled periodic SMI handler. This is an
868 optional parameter that may be NULL. If it is
869 NULL, then the handle will not be returned,
870 which means that the periodic SMI handler can
872 @param[in] DispatchFunction A pointer to a periodic SMI handler function.
873 @param[in] Context Optional content to pass into DispatchFunction.
874 @param[in] TickPeriod The requested tick period in 100ns units that
875 control should be given to the periodic SMI
876 handler. Must be one of the supported values
877 returned by PeriodicSmiSupportedPickPeriod().
878 @param[in] Cpu Specifies the CPU that is required to execute
879 the periodic SMI handler. If Cpu is
880 PERIODIC_SMI_LIBRARY_ANY_CPU, then the periodic
881 SMI handler will always be executed on the SMST
882 CurrentlyExecutingCpu, which may vary across
883 periodic SMIs. If Cpu is between 0 and the SMST
884 NumberOfCpus, then the periodic SMI will always
885 be executed on the requested CPU.
886 @param[in] StackSize The size, in bytes, of the stack to allocate for
887 use by the periodic SMI handler. If 0, then the
888 default stack will be used.
890 @retval EFI_INVALID_PARAMETER DispatchFunction is NULL.
891 @retval EFI_UNSUPPORTED TickPeriod is not a supported tick period. The
892 supported tick periods can be retrieved using
893 PeriodicSmiSupportedTickPeriod().
894 @retval EFI_INVALID_PARAMETER Cpu is not PERIODIC_SMI_LIBRARY_ANY_CPU or in
895 the range 0 to SMST NumberOfCpus.
896 @retval EFI_OUT_OF_RESOURCES There are not enough resources to enable the
897 periodic SMI handler.
898 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate the
899 stack specified by StackSize.
900 @retval EFI_SUCCESS The periodic SMI handler was enabled.
906 IN OUT EFI_HANDLE
*DispatchHandle OPTIONAL
,
907 IN PERIODIC_SMI_LIBRARY_HANDLER DispatchFunction
,
908 IN CONST VOID
*Context OPTIONAL
,
909 IN UINT64 TickPeriod
,
916 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
919 // Make sure all the input parameters are valid
921 if (DispatchFunction
== NULL
) {
922 return EFI_INVALID_PARAMETER
;
925 for (Index
= 0; gSmiTickPeriodTable
[Index
] != 0; Index
++) {
926 if (gSmiTickPeriodTable
[Index
] == TickPeriod
) {
931 if (gSmiTickPeriodTable
[Index
] == 0) {
932 return EFI_UNSUPPORTED
;
935 if ((Cpu
!= PERIODIC_SMI_LIBRARY_ANY_CPU
) && (Cpu
>= gSmst
->NumberOfCpus
)) {
936 return EFI_INVALID_PARAMETER
;
940 // Find a free periodic SMI handler entry
942 PeriodicSmiLibraryHandler
= FindFreePeriodicSmiLibraryHandler ();
943 if (PeriodicSmiLibraryHandler
== NULL
) {
944 return EFI_OUT_OF_RESOURCES
;
948 // Initialize a new periodic SMI handler entry
950 PeriodicSmiLibraryHandler
->YieldFlag
= FALSE
;
951 PeriodicSmiLibraryHandler
->DispatchHandle
= NULL
;
952 PeriodicSmiLibraryHandler
->DispatchFunction
= DispatchFunction
;
953 PeriodicSmiLibraryHandler
->Context
= (VOID
*)Context
;
954 PeriodicSmiLibraryHandler
->Cpu
= Cpu
;
955 PeriodicSmiLibraryHandler
->StackSize
= ALIGN_VALUE (StackSize
, EFI_PAGE_SIZE
);
956 if (PeriodicSmiLibraryHandler
->StackSize
> 0) {
957 PeriodicSmiLibraryHandler
->Stack
= AllocatePages (EFI_SIZE_TO_PAGES (PeriodicSmiLibraryHandler
->StackSize
));
958 if (PeriodicSmiLibraryHandler
->Stack
== NULL
) {
959 return EFI_OUT_OF_RESOURCES
;
962 ZeroMem (PeriodicSmiLibraryHandler
->Stack
, PeriodicSmiLibraryHandler
->StackSize
);
964 PeriodicSmiLibraryHandler
->Stack
= NULL
;
967 InitializeSpinLock (&PeriodicSmiLibraryHandler
->DispatchLock
);
968 PeriodicSmiLibraryHandler
->PerfomanceCounterRate
= GetPerformanceCounterProperties (
969 &PeriodicSmiLibraryHandler
->PerfomanceCounterStartValue
,
970 &PeriodicSmiLibraryHandler
->PerfomanceCounterEndValue
972 PeriodicSmiLibraryHandler
->RegisterContext
.Period
= TickPeriod
;
973 PeriodicSmiLibraryHandler
->RegisterContext
.SmiTickInterval
= TickPeriod
;
974 Status
= gSmmPeriodicTimerDispatch2
->Register (
975 gSmmPeriodicTimerDispatch2
,
976 PeriodicSmiDispatchFunction
,
977 &PeriodicSmiLibraryHandler
->RegisterContext
,
978 &PeriodicSmiLibraryHandler
->DispatchHandle
980 if (EFI_ERROR (Status
)) {
981 PeriodicSmiLibraryHandler
->DispatchHandle
= NULL
;
982 ReclaimPeriodicSmiLibraryHandler (PeriodicSmiLibraryHandler
);
983 return EFI_OUT_OF_RESOURCES
;
987 // Return the registered handle if the optional DispatchHandle parameter is not NULL
989 if (DispatchHandle
!= NULL
) {
990 *DispatchHandle
= PeriodicSmiLibraryHandler
->DispatchHandle
;
997 This function disables a periodic SMI handler that has been previously
998 enabled with PeriodicSmiEnable().
1000 @param[in] DispatchHandle A handle associated with a previously enabled periodic
1001 SMI handler. This is an optional parameter that may
1002 be NULL. If it is NULL, then the active periodic SMI
1003 handlers is disabled.
1005 @retval FALSE DispatchHandle is NULL and there is no active periodic SMI handler.
1006 @retval FALSE The periodic SMI handler specified by DispatchHandle has
1007 not been enabled with PeriodicSmiEnable().
1008 @retval TRUE The periodic SMI handler specified by DispatchHandle has
1009 been disabled. If DispatchHandle is NULL, then the active
1010 periodic SMI handler has been disabled.
1015 PeriodicSmiDisable (
1016 IN EFI_HANDLE DispatchHandle OPTIONAL
1019 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
1023 // Lookup the periodic SMI handler specified by DispatchHandle
1025 PeriodicSmiLibraryHandler
= LookupPeriodicSmiLibraryHandler (DispatchHandle
);
1026 if (PeriodicSmiLibraryHandler
== NULL
) {
1031 // Unregister the periodic SMI handler from the SMM Periodic Timer Dispatch 2 Protocol
1033 Status
= gSmmPeriodicTimerDispatch2
->UnRegister (
1034 gSmmPeriodicTimerDispatch2
,
1035 PeriodicSmiLibraryHandler
->DispatchHandle
1037 if (EFI_ERROR (Status
)) {
1042 // Mark the entry for the disabled periodic SMI handler as free, and
1043 // call ReclaimPeriodicSmiLibraryHandler to move it to the list of free
1044 // periodic SMI handlers.
1046 PeriodicSmiLibraryHandler
->DispatchHandle
= NULL
;
1047 if (PeriodicSmiLibraryHandler
!= GetActivePeriodicSmiLibraryHandler ()) {
1048 ReclaimPeriodicSmiLibraryHandler (PeriodicSmiLibraryHandler
);
1055 This constructor function caches the pointer to the SMM Periodic Timer
1056 Dispatch 2 Protocol and collects the list SMI tick rates that the hardware
1059 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1060 @param[in] SystemTable A pointer to the EFI System Table.
1062 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
1067 SmmPeriodicSmiLibConstructor (
1068 IN EFI_HANDLE ImageHandle
,
1069 IN EFI_SYSTEM_TABLE
*SystemTable
1073 UINT64
*SmiTickInterval
;
1077 // Locate the SMM Periodic Timer Dispatch 2 Protocol
1079 Status
= gSmst
->SmmLocateProtocol (
1080 &gEfiSmmPeriodicTimerDispatch2ProtocolGuid
,
1082 (VOID
**)&gSmmPeriodicTimerDispatch2
1084 ASSERT_EFI_ERROR (Status
);
1085 ASSERT (gSmmPeriodicTimerDispatch2
!= NULL
);
1088 // Count the number of periodic SMI tick intervals that the SMM Periodic Timer
1089 // Dispatch 2 Protocol supports.
1091 SmiTickInterval
= NULL
;
1094 Status
= gSmmPeriodicTimerDispatch2
->GetNextShorterInterval (
1095 gSmmPeriodicTimerDispatch2
,
1099 } while (SmiTickInterval
!= NULL
);
1102 // Allocate a buffer for the table of supported periodic SMI tick periods.
1104 gSmiTickPeriodTable
= AllocateZeroPool (Count
* sizeof (UINT64
));
1105 ASSERT (gSmiTickPeriodTable
!= NULL
);
1108 // Fill in the table of supported periodic SMI tick periods.
1110 SmiTickInterval
= NULL
;
1113 gSmiTickPeriodTable
[Count
] = 0;
1114 Status
= gSmmPeriodicTimerDispatch2
->GetNextShorterInterval (
1115 gSmmPeriodicTimerDispatch2
,
1118 if (SmiTickInterval
!= NULL
) {
1119 gSmiTickPeriodTable
[Count
] = *SmiTickInterval
;
1123 } while (SmiTickInterval
!= NULL
);
1126 // Allocate buffer for initial set of periodic SMI handlers
1128 EnlargeFreePeriodicSmiLibraryHandlerList ();
1134 The constructor function caches the pointer to the SMM Periodic Timer Dispatch 2
1135 Protocol and collects the list SMI tick rates that the hardware supports.
1137 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1138 @param[in] SystemTable A pointer to the EFI System Table.
1140 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
1145 SmmPeriodicSmiLibDestructor (
1146 IN EFI_HANDLE ImageHandle
,
1147 IN EFI_SYSTEM_TABLE
*SystemTable
1151 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
*PeriodicSmiLibraryHandler
;
1154 // Free the table of supported periodic SMI tick rates
1156 if (gSmiTickPeriodTable
!= NULL
) {
1157 FreePool (gSmiTickPeriodTable
);
1161 // Disable all periodic SMI handlers
1163 for (Link
= GetFirstNode (&gPeriodicSmiLibraryHandlers
); !IsNull (&gPeriodicSmiLibraryHandlers
, Link
);) {
1164 PeriodicSmiLibraryHandler
= PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_LINK (Link
);
1165 Link
= GetNextNode (&gPeriodicSmiLibraryHandlers
, Link
);
1166 PeriodicSmiDisable (PeriodicSmiLibraryHandler
->DispatchHandle
);
1170 // Free all the periodic SMI handler entries
1172 for (Link
= GetFirstNode (&gFreePeriodicSmiLibraryHandlers
); !IsNull (&gFreePeriodicSmiLibraryHandlers
, Link
);) {
1173 PeriodicSmiLibraryHandler
= PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_LINK (Link
);
1174 Link
= RemoveEntryList (Link
);
1175 FreePool (PeriodicSmiLibraryHandler
);