1 | /** @file
|
---|
2 | SMM Timer feature support
|
---|
3 |
|
---|
4 | Copyright (c) 2009 - 2024, Intel Corporation. All rights reserved.<BR>
|
---|
5 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
6 |
|
---|
7 | **/
|
---|
8 |
|
---|
9 | #include "PiSmmCpuCommon.h"
|
---|
10 |
|
---|
11 | UINT64 mTimeoutTicker = 0;
|
---|
12 |
|
---|
13 | UINT64 mTimeoutTicker2 = 0;
|
---|
14 |
|
---|
15 | //
|
---|
16 | // Number of counts in a roll-over cycle of the performance counter.
|
---|
17 | //
|
---|
18 | UINT64 mCycle = 0;
|
---|
19 | //
|
---|
20 | // Flag to indicate the performance counter is count-up or count-down.
|
---|
21 | //
|
---|
22 | BOOLEAN mCountDown;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | Initialize Timer for SMM AP Sync.
|
---|
26 |
|
---|
27 | **/
|
---|
28 | VOID
|
---|
29 | InitializeSmmTimer (
|
---|
30 | VOID
|
---|
31 | )
|
---|
32 | {
|
---|
33 | UINT64 TimerFrequency;
|
---|
34 | UINT64 SyncTimeout;
|
---|
35 | UINT64 SyncTimeout2;
|
---|
36 | UINT64 Start;
|
---|
37 | UINT64 End;
|
---|
38 |
|
---|
39 | SyncTimeout = 0;
|
---|
40 | SyncTimeout2 = 0;
|
---|
41 | GetSmmCpuSyncConfigData (NULL, &SyncTimeout, &SyncTimeout2);
|
---|
42 |
|
---|
43 | TimerFrequency = GetPerformanceCounterProperties (&Start, &End);
|
---|
44 | mTimeoutTicker = DivU64x32 (
|
---|
45 | MultU64x64 (TimerFrequency, SyncTimeout),
|
---|
46 | 1000 * 1000
|
---|
47 | );
|
---|
48 | mTimeoutTicker2 = DivU64x32 (
|
---|
49 | MultU64x64 (TimerFrequency, SyncTimeout2),
|
---|
50 | 1000 * 1000
|
---|
51 | );
|
---|
52 | if (End < Start) {
|
---|
53 | mCountDown = TRUE;
|
---|
54 | mCycle = Start - End;
|
---|
55 | } else {
|
---|
56 | mCountDown = FALSE;
|
---|
57 | mCycle = End - Start;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | Start Timer for SMM AP Sync.
|
---|
63 |
|
---|
64 | **/
|
---|
65 | UINT64
|
---|
66 | EFIAPI
|
---|
67 | StartSyncTimer (
|
---|
68 | VOID
|
---|
69 | )
|
---|
70 | {
|
---|
71 | return GetPerformanceCounter ();
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | Check if the SMM AP Sync Timer is timeout specified by Timeout.
|
---|
76 |
|
---|
77 | @param Timer The start timer from the begin.
|
---|
78 | @param Timeout The timeout ticker to wait.
|
---|
79 |
|
---|
80 | **/
|
---|
81 | BOOLEAN
|
---|
82 | EFIAPI
|
---|
83 | IsSyncTimerTimeout (
|
---|
84 | IN UINT64 Timer,
|
---|
85 | IN UINT64 Timeout
|
---|
86 | )
|
---|
87 | {
|
---|
88 | UINT64 CurrentTimer;
|
---|
89 | UINT64 Delta;
|
---|
90 |
|
---|
91 | CurrentTimer = GetPerformanceCounter ();
|
---|
92 | //
|
---|
93 | // We need to consider the case that CurrentTimer is equal to Timer
|
---|
94 | // when some timer runs too slow and CPU runs fast. We think roll over
|
---|
95 | // condition does not happen on this case.
|
---|
96 | //
|
---|
97 | if (mCountDown) {
|
---|
98 | //
|
---|
99 | // The performance counter counts down. Check for roll over condition.
|
---|
100 | //
|
---|
101 | if (CurrentTimer <= Timer) {
|
---|
102 | Delta = Timer - CurrentTimer;
|
---|
103 | } else {
|
---|
104 | //
|
---|
105 | // Handle one roll-over.
|
---|
106 | //
|
---|
107 | Delta = mCycle - (CurrentTimer - Timer) + 1;
|
---|
108 | }
|
---|
109 | } else {
|
---|
110 | //
|
---|
111 | // The performance counter counts up. Check for roll over condition.
|
---|
112 | //
|
---|
113 | if (CurrentTimer >= Timer) {
|
---|
114 | Delta = CurrentTimer - Timer;
|
---|
115 | } else {
|
---|
116 | //
|
---|
117 | // Handle one roll-over.
|
---|
118 | //
|
---|
119 | Delta = mCycle - (Timer - CurrentTimer) + 1;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | return (BOOLEAN)(Delta >= Timeout);
|
---|
124 | }
|
---|