]> git.proxmox.com Git - mirror_edk2.git/blame - SourceLevelDebugPkg/Library/DebugAgent/DebugAgentCommon/DebugTimer.c
SourceLevelDebugPkg/DebugAgent: Disable Debug Timer as early
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugAgent / DebugAgentCommon / DebugTimer.c
CommitLineData
18b144ea 1/** @file\r
2 Code for debug timer to support debug agent library implementation.\r
3\r
08021523 4 Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r
18b144ea 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "DebugAgent.h"\r
16\r
17/**\r
18 Initialize CPU local APIC timer.\r
19\r
99b98734 20 @param[out] TimerFrequency Local APIC timer frequency returned.\r
86d13652 21 @param[in] DumpFlag If TRUE, dump Local APIC timer's parameter.\r
08021523 22 \r
4fe43eb3 23 @return 32-bit Local APIC timer init count.\r
18b144ea 24**/\r
4fe43eb3 25UINT32\r
18b144ea 26InitializeDebugTimer (\r
86d13652
JF
27 OUT UINT32 *TimerFrequency,\r
28 IN BOOLEAN DumpFlag\r
18b144ea 29 )\r
30{\r
31 UINTN ApicTimerDivisor;\r
32 UINT32 InitialCount;\r
08021523 33 UINT32 ApicTimerFrequency;\r
18b144ea 34\r
35 GetApicTimerState (&ApicTimerDivisor, NULL, NULL);\r
08021523 36 ApicTimerFrequency = PcdGet32(PcdFSBClock) / (UINT32)ApicTimerDivisor;\r
18b144ea 37 //\r
38 // Cpu Local Apic timer interrupt frequency, it is set to 0.1s\r
39 //\r
40 InitialCount = (UINT32)DivU64x32 (\r
41 MultU64x64 (\r
08021523
JF
42 ApicTimerFrequency,\r
43 DEBUG_TIMER_INTERVAL\r
18b144ea 44 ),\r
08021523 45 1000000u\r
18b144ea 46 );\r
47\r
48 InitializeApicTimer (ApicTimerDivisor, InitialCount, TRUE, DEBUG_TIMER_VECTOR);\r
2befbc82
JF
49 //\r
50 // Disable Debug Timer interrupt to avoid it is delivered before Debug Port\r
51 // is initialized\r
52 //\r
53 DisableApicTimerInterrupt ();\r
18b144ea 54\r
86d13652
JF
55 if (DumpFlag) {\r
56 DEBUG ((EFI_D_INFO, "Debug Timer: FSB Clock = %d\n", PcdGet32(PcdFSBClock)));\r
57 DEBUG ((EFI_D_INFO, "Debug Timer: Divisor = %d\n", ApicTimerDivisor));\r
58 DEBUG ((EFI_D_INFO, "Debug Timer: Frequency = %d\n", ApicTimerFrequency));\r
59 DEBUG ((EFI_D_INFO, "Debug Timer: InitialCount = %d\n", InitialCount));\r
60 }\r
08021523
JF
61 if (TimerFrequency != NULL) {\r
62 *TimerFrequency = ApicTimerFrequency;\r
63 }\r
4fe43eb3 64 return InitialCount;\r
18b144ea 65}\r
66\r
67/**\r
68 Enable/Disable the interrupt of debug timer and return the interrupt state\r
69 prior to the operation.\r
70\r
71 If EnableStatus is TRUE, enable the interrupt of debug timer.\r
72 If EnableStatus is FALSE, disable the interrupt of debug timer.\r
73\r
74 @param[in] EnableStatus Enable/Disable.\r
75\r
76 @retval TRUE Debug timer interrupt were enabled on entry to this call.\r
77 @retval FALSE Debug timer interrupt were disabled on entry to this call.\r
78\r
79**/\r
80BOOLEAN\r
81EFIAPI\r
82SaveAndSetDebugTimerInterrupt (\r
83 IN BOOLEAN EnableStatus\r
84 )\r
85{\r
18b144ea 86 BOOLEAN OldDebugTimerInterruptState;\r
87\r
18b144ea 88 OldDebugTimerInterruptState = GetApicTimerInterruptState ();\r
4fe43eb3 89\r
b422b62c 90 if (OldDebugTimerInterruptState != EnableStatus) {\r
91 if (EnableStatus) {\r
92 EnableApicTimerInterrupt ();\r
93 } else {\r
94 DisableApicTimerInterrupt ();\r
95 }\r
96 //\r
97 // Validate the Debug Timer interrupt state\r
98 // This will make additional delay after Local Apic Timer interrupt state is changed.\r
99 // Thus, CPU could handle the potential pending interrupt of Local Apic timer.\r
100 //\r
101 while (GetApicTimerInterruptState () != EnableStatus) {\r
102 CpuPause ();\r
103 }\r
18b144ea 104 }\r
4fe43eb3 105\r
18b144ea 106 return OldDebugTimerInterruptState;\r
107}\r
108\r
08021523
JF
109/**\r
110 Check if the timer is time out.\r
111 \r
112 @param[in] TimerCycle Timer total count.\r
113 @param[in] Timer The start timer from the begin.\r
114 @param[in] TimeoutTicker Ticker number need time out.\r
115\r
116 @return TRUE Timer time out occurs.\r
117 @retval FALSE Timer does not time out.\r
118\r
119**/\r
120BOOLEAN\r
121IsDebugTimerTimeout (\r
122 IN UINT32 TimerCycle,\r
123 IN UINT32 Timer,\r
124 IN UINT32 TimeoutTicker\r
125 )\r
126{\r
127 UINT64 CurrentTimer;\r
128 UINT64 Delta;\r
129\r
130 CurrentTimer = GetApicTimerCurrentCount ();\r
131\r
132 //\r
133 // This timer counter counts down. Check for roll over condition.\r
134 //\r
135 if (CurrentTimer < Timer) {\r
136 Delta = Timer - CurrentTimer;\r
137 } else {\r
138 //\r
139 // Handle one roll-over. \r
140 //\r
141 Delta = TimerCycle - (CurrentTimer - Timer);\r
142 }\r
143 \r
144 return (BOOLEAN) (Delta >= TimeoutTicker);\r
145}\r
146\r