]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/SmmPeriodicSmiLib/SmmPeriodicSmiLib.c
Add SmmPeriodicSmiLib to MdePkg.
[mirror_edk2.git] / MdePkg / Library / SmmPeriodicSmiLib / SmmPeriodicSmiLib.c
1 /** @file
2 SMM Periodic SMI Library.
3
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.
9
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.
12
13 **/
14
15 #include <PiSmm.h>
16
17 #include <Protocol/SmmPeriodicTimerDispatch2.h>
18
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>
26
27 #include <Library/SmmPeriodicSmiLib.h>
28
29 ///
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
33 /// is full.
34 ///
35 #define PERIODIC_SMI_LIBRARY_ALLOCATE_SIZE 0x08
36
37 ///
38 /// Signature for a PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure
39 ///
40 #define PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE SIGNATURE_32 ('P', 'S', 'M', 'I')
41
42 ///
43 /// Structure that contains state information for an enabled periodic SMI handler
44 ///
45 typedef struct {
46 ///
47 /// Signature value that must be set to PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE
48 ///
49 UINT32 Signature;
50 ///
51 /// The dispatch function to called to invoke an enabled periodic SMI handler.
52 ///
53 PERIODIC_SMI_LIBRARY_HANDLER DispatchFunction;
54 ///
55 /// The context to pass into DispatchFunction
56 ///
57 VOID *Context;
58 ///
59 /// The tick period in 100 ns units that DispatchFunction should be called.
60 ///
61 UINT64 TickPeriod;
62 ///
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
65 /// on any CPU.
66 ///
67 UINTN Cpu;
68 ///
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.
71 ///
72 UINTN StackSize;
73 ///
74 /// A pointer to the stack allocated using AllocatePages(). This field will
75 /// be NULL if StackSize is 0.
76 ///
77 VOID *Stack;
78 ///
79 /// Spin lock used to wait for an AP to complete the execution of a periodic SMI handler
80 ///
81 SPIN_LOCK DispatchLock;
82 ///
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.
85 ///
86 UINT64 PerfomanceCounterRate;
87 ///
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.
90 ///
91 UINT64 PerfomanceCounterStartValue;
92 ///
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.
95 ///
96 UINT64 PerfomanceCounterEndValue;
97 ///
98 /// The context record passed into the Register() function of the SMM Periodic
99 /// Timer Dispatch Protocol when a periodic SMI handler is enabled.
100 ///
101 EFI_SMM_PERIODIC_TIMER_REGISTER_CONTEXT RegisterContext;
102 ///
103 /// The handle returned from the Register() function of the SMM Periodic
104 /// Timer Dispatch Protocol when a periodic SMI handler is enabled.
105 ///
106 EFI_HANDLE DispatchHandle;
107 ///
108 /// The total number of performance counter ticks that the periodic SMI handler
109 /// has been executing in its current invocation.
110 ///
111 UINT64 DispatchTotalTime;
112 ///
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.
117 ///
118 UINT64 DispatchCheckPointTime;
119 ///
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.
123 ///
124 BASE_LIBRARY_JUMP_BUFFER DispatchJumpBuffer;
125 ///
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.
129 ///
130 BOOLEAN YieldFlag;
131 ///
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.
136 ///
137 BASE_LIBRARY_JUMP_BUFFER YieldJumpBuffer;
138 ///
139 /// The amount of time, in 100 ns units, that have elapsed since the last
140 /// time the periodic SMI handler was invoked.
141 ///
142 UINT64 ElapsedTime;
143 } PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT;
144
145 /**
146 Macro that returns a pointer to a PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
147 structure based on a pointer to a RegisterContext field.
148
149 **/
150 #define PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_REGISTER_CONTEXT(a) \
151 CR ( \
152 a, \
153 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT, \
154 RegisterContext, \
155 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE \
156 )
157
158 ///
159 /// Pointer to the SMM Periodic Timer Disatch Protocol that was located in the constuctor.
160 ///
161 EFI_SMM_PERIODIC_TIMER_DISPATCH2_PROTOCOL *gSmmPeriodicTimerDispatch2 = NULL;
162
163 ///
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().
169 ///
170 UINT64 *gSmiTickPeriodTable = NULL;
171
172 ///
173 /// The number entries in gPeriodicSmiLibraryHandlers
174 ///
175 UINTN gNumberOfPeriodicSmiLibraryHandlers = 0;
176
177 ///
178 /// Table of periodic SMI handlers that this library is currently managing. This
179 /// table is allocated using AllocatePool()
180 ///
181 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *gPeriodicSmiLibraryHandlers = NULL;
182
183 ///
184 /// The index of gPeriodicSmiLibraryHandlers that is currently being executed.
185 /// Is set to -1 if no periodic SMI handler is currently being executed.
186 ///
187 INTN gActivePeriodicSmiLibraryHandlerIndex = -1;
188
189 /**
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.
194
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.
198
199 **/
200 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *
201 GetActivePeriodicSmiLibraryHandler (
202 VOID
203 )
204 {
205 if (gActivePeriodicSmiLibraryHandlerIndex < 0) {
206 //
207 // Return NULL if index is negative, which means that there is no active
208 // periodic SMI handler.
209 //
210 return NULL;
211 }
212
213 //
214 // Return a pointer to the active periodic SMI handler context
215 //
216 return &gPeriodicSmiLibraryHandlers[gActivePeriodicSmiLibraryHandlerIndex];
217 }
218
219 /**
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.
226
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.
232
233 @retval NULL DispatchHandle is NULL and there is no active periodic SMI
234 handler.
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.
239
240 **/
241 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *
242 LookupPeriodicSmiLibraryHandler (
243 IN EFI_HANDLE DispatchHandle OPTIONAL
244 )
245 {
246 UINTN Index;
247
248 //
249 // If DispatchHandle is NULL, then return the active periodic SMI handler
250 //
251 if (DispatchHandle == NULL) {
252 return GetActivePeriodicSmiLibraryHandler ();
253 }
254
255 //
256 // Search the periodic SMI handler entries for a a matching DispatchHandle
257 //
258 for (Index = 0; Index < gNumberOfPeriodicSmiLibraryHandlers; Index++) {
259 if (gPeriodicSmiLibraryHandlers[Index].DispatchHandle == DispatchHandle) {
260 return &gPeriodicSmiLibraryHandlers[Index];
261 }
262 }
263
264 //
265 // No entries match DispatchHandle, so return NULL
266 //
267 return NULL;
268 }
269
270 /**
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
276 is returned.
277
278 @retval NULL Context is NULL.
279 @retval other Pointer to the PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
280 associated with Context.
281
282 **/
283 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *
284 SetActivePeriodicSmiLibraryHandler (
285 IN CONST VOID *Context OPTIONAL
286 )
287 {
288 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
289
290 if (Context == NULL) {
291 gActivePeriodicSmiLibraryHandlerIndex = -1;
292 return NULL;
293 }
294 PeriodicSmiLibraryHandler = PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_REGISTER_CONTEXT (Context);
295 gActivePeriodicSmiLibraryHandlerIndex = PeriodicSmiLibraryHandler - gPeriodicSmiLibraryHandlers;
296 return PeriodicSmiLibraryHandler;
297 }
298
299 /**
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.
303
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.
306
307 **/
308 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *
309 FindFreePeriodicSmiLibraryHandler (
310 VOID
311 )
312 {
313 UINTN Index;
314 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
315
316 //
317 // Search for a free entry in gPeriodicSmiLibraryHandlers
318 // A free entry must have a NULL DispatchHandle and a NULL Stack.
319 //
320 for (Index = 0; Index < gNumberOfPeriodicSmiLibraryHandlers; Index++) {
321 if (gPeriodicSmiLibraryHandlers[Index].DispatchHandle != NULL) {
322 continue;
323 }
324 if (gPeriodicSmiLibraryHandlers[Index].Stack != NULL) {
325 continue;
326 }
327 return &gPeriodicSmiLibraryHandlers[Index];
328 }
329
330 //
331 // If no free entries were found, then grow the table of periodic SMI handler entries
332 //
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
338 );
339 if (PeriodicSmiLibraryHandler == NULL) {
340 return NULL;
341 }
342 gPeriodicSmiLibraryHandlers = PeriodicSmiLibraryHandler;
343 gNumberOfPeriodicSmiLibraryHandlers += PERIODIC_SMI_LIBRARY_ALLOCATE_SIZE;
344 }
345
346 return &gPeriodicSmiLibraryHandlers[Index];
347 }
348
349 /**
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.
355
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.
359
360 **/
361 UINT64 *
362 EFIAPI
363 PeriodicSmiSupportedTickPeriod (
364 VOID
365 )
366 {
367 //
368 // Return the table allocated and populated by SmmPeriodicSmiLibConstructor()
369 //
370 return gSmiTickPeriodTable;
371 }
372
373 /**
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.
378
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.
382
383 **/
384 UINT64
385 EFIAPI
386 PeriodicSmiExecutionTime (
387 VOID
388 )
389 {
390 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
391 UINT64 Current;
392 UINT64 Count;
393
394 //
395 // If there is no active periodic SMI handler, then return 0
396 //
397 PeriodicSmiLibraryHandler = GetActivePeriodicSmiLibraryHandler ();
398 if (PeriodicSmiLibraryHandler == NULL) {
399 return 0;
400 }
401
402 //
403 // Get the current performance counter value
404 //
405 Current = GetPerformanceCounter ();
406
407 //
408 // Count the number of performance counter ticks since the periodic SMI handler
409 // was dispatched or the last time this function was called.
410 //
411 if (PeriodicSmiLibraryHandler->PerfomanceCounterEndValue > PeriodicSmiLibraryHandler->PerfomanceCounterStartValue) {
412 //
413 // The performance counter counts up. Check for roll over condition.
414 //
415 if (Current > PeriodicSmiLibraryHandler->DispatchCheckPointTime) {
416 Count = Current - PeriodicSmiLibraryHandler->DispatchCheckPointTime;
417 } else {
418 Count = (Current - PeriodicSmiLibraryHandler->PerfomanceCounterStartValue) + (PeriodicSmiLibraryHandler->PerfomanceCounterEndValue - PeriodicSmiLibraryHandler->DispatchCheckPointTime);
419 }
420 } else {
421 //
422 // The performance counter counts down. Check for roll over condition.
423 //
424 if (PeriodicSmiLibraryHandler->DispatchCheckPointTime > Current) {
425 Count = PeriodicSmiLibraryHandler->DispatchCheckPointTime - Current;
426 } else {
427 Count = (PeriodicSmiLibraryHandler->DispatchCheckPointTime - PeriodicSmiLibraryHandler->PerfomanceCounterEndValue) + (PeriodicSmiLibraryHandler->PerfomanceCounterStartValue - Current);
428 }
429 }
430
431 //
432 // Accumulate the total number of performance counter ticks since the periodic
433 // SMI handler was dispatched or resumed.
434 //
435 PeriodicSmiLibraryHandler->DispatchTotalTime += Count;
436
437 //
438 // Update the checkpoint value to the current performance counter value
439 //
440 PeriodicSmiLibraryHandler->DispatchCheckPointTime = Current;
441
442 //
443 // Convert the total number of performance counter ticks to 100 ns units
444 //
445 return DivU64x64Remainder (
446 MultU64x32 (PeriodicSmiLibraryHandler->DispatchTotalTime, 10000000),
447 PeriodicSmiLibraryHandler->PerfomanceCounterRate,
448 NULL
449 );
450 }
451
452 /**
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.
458
459 **/
460 VOID
461 EFIAPI
462 PeriodicSmiExit (
463 VOID
464 )
465 {
466 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
467
468 //
469 // If there is no active periodic SMI handler, then return
470 //
471 PeriodicSmiLibraryHandler = GetActivePeriodicSmiLibraryHandler ();
472 if (PeriodicSmiLibraryHandler == NULL) {
473 return;
474 }
475
476 //
477 // Perform a long jump back to the point when the currently executing dispatch
478 // function was dispatched.
479 //
480 LongJump (&PeriodicSmiLibraryHandler->DispatchJumpBuffer, 1);
481
482 //
483 // Must never return
484 //
485 ASSERT (FALSE);
486 CpuDeadLoop();
487 }
488
489 /**
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
495 used.
496
497 If the stack size passed into PeriodicSmiEnable() was zero, the 0 is returned.
498
499 If this function is not called from within an enabled periodic SMI handler,
500 then 0 is returned.
501
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.
504
505 **/
506 UINT64
507 EFIAPI
508 PeriodicSmiYield (
509 VOID
510 )
511 {
512 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
513 UINTN SetJumpFlag;
514
515 //
516 // If there is no active periodic SMI handler, then return
517 //
518 PeriodicSmiLibraryHandler = GetActivePeriodicSmiLibraryHandler ();
519 if (PeriodicSmiLibraryHandler == NULL) {
520 return 0;
521 }
522
523 //
524 // If PeriodicSmiYield() is called without an allocated stack, then just return
525 // immediately with an elapsed time of 0.
526 //
527 if (PeriodicSmiLibraryHandler->Stack == NULL) {
528 return 0;
529 }
530
531 //
532 // Set a flag so the next periodic SMI event will resume at where SetJump()
533 // is called below.
534 //
535 PeriodicSmiLibraryHandler->YieldFlag = TRUE;
536
537 //
538 // Save context in YieldJumpBuffer
539 //
540 SetJumpFlag = SetJump (&PeriodicSmiLibraryHandler->YieldJumpBuffer);
541 if (SetJumpFlag == 0) {
542 //
543 // The intial call to SetJump() always returns 0.
544 // If this is the initial call, then exit the current periodic SMI handler
545 //
546 PeriodicSmiExit ();
547 }
548
549 //
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.
553 //
554 // Clear the flag so the next periodic SMI dispatch will not resume.
555 //
556 PeriodicSmiLibraryHandler->YieldFlag = FALSE;
557
558 //
559 // Return the amount elapsed time that occured while yielded
560 //
561 return PeriodicSmiLibraryHandler->ElapsedTime;
562 }
563
564 /**
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().
569
570 @param[in] Context1 Context1 parameter passed into SwitchStack().
571 @param[in] Context2 Context2 parameter passed into SwitchStack().
572
573 **/
574 VOID
575 EFIAPI
576 PeriodicSmiDispatchFunctionSwitchStack (
577 IN VOID *Context1, OPTIONAL
578 IN VOID *Context2 OPTIONAL
579 )
580 {
581 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
582
583 //
584 // Convert Context1 to PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *
585 //
586 PeriodicSmiLibraryHandler = (PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *)Context1;
587
588 //
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.
592 //
593 PeriodicSmiLibraryHandler->DispatchFunction (
594 PeriodicSmiLibraryHandler->Context,
595 PeriodicSmiLibraryHandler->ElapsedTime
596 );
597
598 //
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.
602 //
603 PeriodicSmiExit ();
604 }
605
606 /**
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().
611
612 @param[in] PeriodicSmiLibraryHandler A pointer to the context for the periodic
613 SMI handler to execute.
614
615 **/
616 VOID
617 EFIAPI
618 PeriodicSmiDispatchFunctionOnCpu (
619 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler
620 )
621 {
622 //
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.
627 //
628 if (SetJump (&PeriodicSmiLibraryHandler->DispatchJumpBuffer) != 0) {
629 return;
630 }
631
632 //
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
635 // calculated.
636 //
637 PeriodicSmiLibraryHandler->DispatchTotalTime = 0;
638 PeriodicSmiLibraryHandler->DispatchCheckPointTime = GetPerformanceCounter();
639
640 if (PeriodicSmiLibraryHandler->YieldFlag) {
641 //
642 // Perform a long jump back to the point where the previously dispatched
643 // function called PeriodicSmiYield().
644 //
645 LongJump (&PeriodicSmiLibraryHandler->YieldJumpBuffer, 1);
646 } else if (PeriodicSmiLibraryHandler->Stack == NULL) {
647 //
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.
651 //
652 PeriodicSmiLibraryHandler->DispatchFunction (
653 PeriodicSmiLibraryHandler->Context,
654 PeriodicSmiLibraryHandler->ElapsedTime
655 );
656
657 //
658 // If this DispatchFunction() returns, then unconditially call PeriodicSmiExit()
659 // to perform a LongJump() back to this function.
660 //
661 PeriodicSmiExit ();
662 } else {
663 //
664 // If Stack is not NULL then call DispatchFunction switching to the allocated stack
665 //
666 SwitchStack (
667 PeriodicSmiDispatchFunctionSwitchStack,
668 PeriodicSmiLibraryHandler,
669 NULL,
670 (UINT8 *)PeriodicSmiLibraryHandler->Stack + PeriodicSmiLibraryHandler->StackSize
671 );
672 }
673
674 //
675 // Must never return
676 //
677 ASSERT (FALSE);
678 CpuDeadLoop();
679 }
680
681 /**
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.
689
690 @param[in] Buffer A pointer to the context for the periodic SMI handler.
691
692 **/
693 VOID
694 EFIAPI
695 PeriodicSmiDispatchFunctionWithLock (
696 IN OUT VOID *Buffer
697 )
698 {
699 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
700
701 //
702 // Get context
703 //
704 PeriodicSmiLibraryHandler = (PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *)Buffer;
705
706 //
707 // Execute dispatch function on the currently excuting logical CPU
708 //
709 PeriodicSmiDispatchFunctionOnCpu (PeriodicSmiLibraryHandler);
710
711 //
712 // Release the dispatch spin lock
713 //
714 ReleaseSpinLock (&PeriodicSmiLibraryHandler->DispatchLock);
715 }
716
717 /**
718 Internal worker function that transfers control to a periodic SMI handler that
719 was enabled using PeriodicSmiEnable().
720
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
727 an SMM environment.
728 @param[in,out] CommBufferSize The size of the CommBuffer.
729
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.
737
738 **/
739 EFI_STATUS
740 EFIAPI
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
746 )
747 {
748 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
749 EFI_SMM_PERIODIC_TIMER_CONTEXT *TimerContext;
750 EFI_STATUS Status;
751
752 //
753 // Set the active periodic SMI handler
754 //
755 PeriodicSmiLibraryHandler = SetActivePeriodicSmiLibraryHandler (Context);
756 if (PeriodicSmiLibraryHandler == NULL) {
757 return EFI_NOT_FOUND;
758 }
759
760 //
761 // Retrieve the elapsed time since the last time this periodic SMI handler was called
762 //
763 PeriodicSmiLibraryHandler->ElapsedTime = 0;
764 if (CommBuffer != NULL) {
765 TimerContext = (EFI_SMM_PERIODIC_TIMER_CONTEXT *)CommBuffer;
766 PeriodicSmiLibraryHandler->ElapsedTime = TimerContext->ElapsedTime;
767 }
768
769 //
770 // Dispatch the periodic SMI handler
771 //
772 if ((PeriodicSmiLibraryHandler->Cpu == PERIODIC_SMI_LIBRARY_ANY_CPU) ||
773 (PeriodicSmiLibraryHandler->Cpu == gSmst->CurrentlyExecutingCpu) ) {
774 //
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().
778 //
779 PeriodicSmiDispatchFunctionOnCpu (PeriodicSmiLibraryHandler);
780 } else {
781 //
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.
784 //
785 AcquireSpinLock (&PeriodicSmiLibraryHandler->DispatchLock);
786
787 //
788 // Execute the periodic SMI handler on the CPU that was specified in
789 // PeriodicSmiEnable().
790 //
791 Status = gSmst->SmmStartupThisAp (
792 PeriodicSmiDispatchFunctionWithLock,
793 PeriodicSmiLibraryHandler->Cpu,
794 PeriodicSmiLibraryHandler
795 );
796 if (!EFI_ERROR (Status)) {
797 //
798 // Wait for the AP to release the spin lock.
799 //
800 while (!AcquireSpinLockOrFail (&PeriodicSmiLibraryHandler->DispatchLock)) {
801 CpuPause ();
802 }
803 }
804
805 //
806 // Release the spin lock for the periodic SMI handler.
807 //
808 ReleaseSpinLock (&PeriodicSmiLibraryHandler->DispatchLock);
809 }
810
811 //
812 // Retrieve the active periodic SMI handler in case the entries were reallocated
813 // when the active periodic SMI handler was dispatched.
814 //
815 PeriodicSmiLibraryHandler = GetActivePeriodicSmiLibraryHandler ();
816 if (PeriodicSmiLibraryHandler != NULL) {
817 //
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.
821 //
822 if (PeriodicSmiLibraryHandler->DispatchHandle == NULL) {
823 if (PeriodicSmiLibraryHandler->Stack != NULL) {
824 FreePages (
825 PeriodicSmiLibraryHandler->Stack,
826 EFI_SIZE_TO_PAGES (PeriodicSmiLibraryHandler->StackSize)
827 );
828 PeriodicSmiLibraryHandler->Stack = NULL;
829 }
830 }
831 }
832
833 //
834 // Update state to show that there is no active periodic SMI handler
835 //
836 SetActivePeriodicSmiLibraryHandler (NULL);
837
838 return EFI_SUCCESS;
839 }
840
841 /**
842 This function enables a periodic SMI handler.
843
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
849 never be disabled.
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.
867
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.
879
880 **/
881 EFI_STATUS
882 EFIAPI
883 PeriodicSmiEnable (
884 IN OUT EFI_HANDLE *DispatchHandle, OPTIONAL
885 IN PERIODIC_SMI_LIBRARY_HANDLER DispatchFunction,
886 IN CONST VOID *Context, OPTIONAL
887 IN UINT64 TickPeriod,
888 IN UINTN Cpu,
889 IN UINTN StackSize
890 )
891 {
892 EFI_STATUS Status;
893 UINTN Index;
894 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
895
896 //
897 // Make sure all the input parameters are valid
898 //
899 if (DispatchFunction == NULL) {
900 return EFI_INVALID_PARAMETER;
901 }
902
903 for (Index = 0; gSmiTickPeriodTable[Index] != 0; Index++) {
904 if (gSmiTickPeriodTable[Index] == TickPeriod) {
905 break;
906 }
907 }
908 if (gSmiTickPeriodTable[Index] == 0) {
909 return EFI_UNSUPPORTED;
910 }
911
912 if (Cpu != PERIODIC_SMI_LIBRARY_ANY_CPU && Cpu >= gSmst->NumberOfCpus) {
913 return EFI_INVALID_PARAMETER;
914 }
915
916 //
917 // Find a free periodic SMI handler entry
918 //
919 PeriodicSmiLibraryHandler = FindFreePeriodicSmiLibraryHandler();
920 if (PeriodicSmiLibraryHandler == NULL) {
921 return EFI_OUT_OF_RESOURCES;
922 }
923
924 //
925 // Initialize a new periodic SMI handler entry
926 //
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;
938 }
939 ZeroMem (PeriodicSmiLibraryHandler->Stack, PeriodicSmiLibraryHandler->StackSize);
940 }
941 InitializeSpinLock (&PeriodicSmiLibraryHandler->DispatchLock);
942 PeriodicSmiLibraryHandler->PerfomanceCounterRate = GetPerformanceCounterProperties (
943 &PeriodicSmiLibraryHandler->PerfomanceCounterStartValue,
944 &PeriodicSmiLibraryHandler->PerfomanceCounterEndValue
945 );
946 PeriodicSmiLibraryHandler->RegisterContext.Period = TickPeriod;
947 PeriodicSmiLibraryHandler->RegisterContext.SmiTickInterval = TickPeriod;
948 Status = gSmmPeriodicTimerDispatch2->Register (
949 gSmmPeriodicTimerDispatch2,
950 PeriodicSmiDispatchFunction,
951 &PeriodicSmiLibraryHandler->RegisterContext,
952 &PeriodicSmiLibraryHandler->DispatchHandle
953 );
954 if (EFI_ERROR (Status) || PeriodicSmiLibraryHandler->DispatchHandle == NULL) {
955 //
956 // If the registration failed or the handle is invalid, free the stack if one was allocated
957 //
958 if (PeriodicSmiLibraryHandler->Stack != NULL) {
959 FreePages (
960 PeriodicSmiLibraryHandler->Stack,
961 EFI_SIZE_TO_PAGES (PeriodicSmiLibraryHandler->StackSize)
962 );
963 PeriodicSmiLibraryHandler->Stack = NULL;
964 }
965 PeriodicSmiLibraryHandler->DispatchHandle = NULL;
966 return EFI_OUT_OF_RESOURCES;
967 }
968
969 //
970 // Return the registered handle if the optional DispatchHandle parameter is not NULL
971 //
972 if (DispatchHandle != NULL) {
973 *DispatchHandle = PeriodicSmiLibraryHandler->DispatchHandle;
974 }
975 return EFI_SUCCESS;
976 }
977
978 /**
979 This function disables a periodic SMI handler that has been previously
980 enabled with PeriodicSmiEnable().
981
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.
986
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.
993
994 **/
995 BOOLEAN
996 EFIAPI
997 PeriodicSmiDisable (
998 IN EFI_HANDLE DispatchHandle OPTIONAL
999 )
1000 {
1001 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *PeriodicSmiLibraryHandler;
1002 EFI_STATUS Status;
1003
1004 //
1005 // Lookup the periodic SMI handler specified by DispatchHandle
1006 //
1007 PeriodicSmiLibraryHandler = LookupPeriodicSmiLibraryHandler (DispatchHandle);
1008 if (PeriodicSmiLibraryHandler == NULL) {
1009 return FALSE;
1010 }
1011
1012 //
1013 // Unregister the periodic SMI handler from the SMM Periodic Timer Dispatch 2 Protocol
1014 //
1015 Status = gSmmPeriodicTimerDispatch2->UnRegister (
1016 gSmmPeriodicTimerDispatch2,
1017 PeriodicSmiLibraryHandler->DispatchHandle
1018 );
1019 if (EFI_ERROR (Status)) {
1020 return FALSE;
1021 }
1022
1023 //
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.
1027 //
1028 if (PeriodicSmiLibraryHandler != GetActivePeriodicSmiLibraryHandler ()) {
1029 if (PeriodicSmiLibraryHandler->Stack != NULL) {
1030 FreePages (
1031 PeriodicSmiLibraryHandler->Stack,
1032 EFI_SIZE_TO_PAGES (PeriodicSmiLibraryHandler->StackSize)
1033 );
1034 PeriodicSmiLibraryHandler->Stack = NULL;
1035 }
1036 }
1037
1038 //
1039 // Mark the entry for the disabled periodic SMI handler as free
1040 //
1041 PeriodicSmiLibraryHandler->DispatchHandle = NULL;
1042
1043 return TRUE;
1044 }
1045
1046 /**
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
1049 supports.
1050
1051 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1052 @param[in] SystemTable A pointer to the EFI System Table.
1053
1054 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
1055
1056 **/
1057 EFI_STATUS
1058 EFIAPI
1059 SmmPeriodicSmiLibConstructor (
1060 IN EFI_HANDLE ImageHandle,
1061 IN EFI_SYSTEM_TABLE *SystemTable
1062 )
1063 {
1064 EFI_STATUS Status;
1065 UINT64 *SmiTickInterval;
1066 UINTN Count;
1067
1068 //
1069 // Locate the SMM Periodic Timer Dispatch 2 Protocol
1070 //
1071 Status = gSmst->SmmLocateProtocol (
1072 &gEfiSmmPeriodicTimerDispatch2ProtocolGuid,
1073 NULL,
1074 (VOID **)&gSmmPeriodicTimerDispatch2
1075 );
1076 ASSERT_EFI_ERROR (Status);
1077 ASSERT (gSmmPeriodicTimerDispatch2 != NULL);
1078
1079 //
1080 // Count the number of periodic SMI tick intervals that the SMM Periodic Timer
1081 // Dipatch 2 Protocol supports.
1082 //
1083 SmiTickInterval = NULL;
1084 Count = 0;
1085 do {
1086 Status = gSmmPeriodicTimerDispatch2->GetNextShorterInterval (
1087 gSmmPeriodicTimerDispatch2,
1088 &SmiTickInterval
1089 );
1090 Count++;
1091 } while (SmiTickInterval != NULL);
1092
1093 //
1094 // Allocate a buffer for the table of supported periodic SMI tick periods.
1095 //
1096 gSmiTickPeriodTable = AllocateZeroPool (Count * sizeof (UINT64));
1097 ASSERT (gSmiTickPeriodTable != NULL);
1098
1099 //
1100 // Fill in the table of supported periodic SMI tick periods.
1101 //
1102 SmiTickInterval = NULL;
1103 Count = 0;
1104 do {
1105 gSmiTickPeriodTable[Count] = 0;
1106 Status = gSmmPeriodicTimerDispatch2->GetNextShorterInterval (
1107 gSmmPeriodicTimerDispatch2,
1108 &SmiTickInterval
1109 );
1110 if (SmiTickInterval != NULL) {
1111 gSmiTickPeriodTable[Count] = *SmiTickInterval;
1112 }
1113 Count++;
1114 } while (SmiTickInterval != NULL);
1115
1116 //
1117 // Allocate buffer for initial set of periodic SMI handlers
1118 //
1119 FindFreePeriodicSmiLibraryHandler ();
1120
1121 return EFI_SUCCESS;
1122 }
1123
1124 /**
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.
1127
1128 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1129 @param[in] SystemTable A pointer to the EFI System Table.
1130
1131 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
1132
1133 **/
1134 EFI_STATUS
1135 EFIAPI
1136 SmmPeriodicSmiLibDestructor (
1137 IN EFI_HANDLE ImageHandle,
1138 IN EFI_SYSTEM_TABLE *SystemTable
1139 )
1140 {
1141 UINTN Index;
1142
1143 //
1144 // Free the table of supported periodic SMI tick rates
1145 //
1146 if (gSmiTickPeriodTable != NULL) {
1147 FreePool (gSmiTickPeriodTable);
1148 }
1149
1150 //
1151 // Disable all periodic SMI handlers
1152 //
1153 for (Index = 0; Index < gNumberOfPeriodicSmiLibraryHandlers; Index++) {
1154 PeriodicSmiDisable (gPeriodicSmiLibraryHandlers[Index].DispatchHandle);
1155 }
1156
1157 //
1158 // Free all the periodic SMI handler entries
1159 //
1160 if (gPeriodicSmiLibraryHandlers != NULL) {
1161 FreePool (gPeriodicSmiLibraryHandlers);
1162 }
1163
1164 return EFI_SUCCESS;
1165 }