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